Generating .pdf files with batch?
Moderator: DosItHelp
Generating .pdf files with batch?
Hello!
Anyone has ever tried to generate .pdf files with batch? I would really like to generate report files...
Thanks.
Saso
Anyone has ever tried to generate .pdf files with batch? I would really like to generate report files...
Thanks.
Saso
Re: Generating .pdf files with batch?
Hi Saso!
Can you please be more specific? Is the content of such a report file only plain text? Or is the source rather a specific file format like MS Word .doc?
Steffen
Can you please be more specific? Is the content of such a report file only plain text? Or is the source rather a specific file format like MS Word .doc?
Steffen
Re: Generating .pdf files with batch?
Hello Steffen!
Text only. It would be a report that is generally created with ECHO.
I looked at .pdf specifications but too complex for me. For me it would be easier to:
- have a program to generate .pdf files from the, for example, text in notepad or .txt file (but not via Print feature - native)
Something like this (I usualy generate reports in this way with batch):
- maybe based on that it would be possible to see what is in .pdf file (binary structure).
Thanks.
Saso
Text only. It would be a report that is generally created with ECHO.
I looked at .pdf specifications but too complex for me. For me it would be easier to:
- have a program to generate .pdf files from the, for example, text in notepad or .txt file (but not via Print feature - native)
Something like this (I usualy generate reports in this way with batch):
Code: Select all
Database check report generated on 15-dec-2017 at 13:00.
ERR1 User1 Location
ERR1 User2 Location
ERR2 User3 Location
ERR1 User4 Location
ERR3 User5 Location
End of database report.
Thanks.
Saso
Re: Generating .pdf files with batch?
I found a very old tool that still works on my Win10 (thus, I assume it will also work on XP).
http://www.eprg.org/pdfcorner/text2pdf/
I downloaded the "Windows 95 (and NT?)" version and renamed the executable to "text2pdf.exe".
That would work, too:
Steffen
PS: Checked on VirusTotal
https://www.virustotal.com/en/file/c71c ... /analysis/
PPS: Works with ASCII only. No characters >7 Bits supported.
http://www.eprg.org/pdfcorner/text2pdf/
I downloaded the "Windows 95 (and NT?)" version and renamed the executable to "text2pdf.exe".
Code: Select all
text2pdf "myfile.txt" > "myfile.pdf"
Code: Select all
(
echo Hello
echo World
)|text2pdf > "myfile.pdf"
PS: Checked on VirusTotal
https://www.virustotal.com/en/file/c71c ... /analysis/
PPS: Works with ASCII only. No characters >7 Bits supported.
Re: Generating .pdf files with batch?
Wow! Thanks Steffen for finding it.
It actually works great. Also has lots of parameters.
.C source is also on the site. Problems might be 8-bit characters (national characters).
So porting it to .cmd is next...
Thanks.
Saso
It actually works great. Also has lots of parameters.
.C source is also on the site. Problems might be 8-bit characters (national characters).
So porting it to .cmd is next...
Thanks.
Saso
Re: Generating .pdf files with batch?
I already saw it but I'm not familiar with the PDF specification. That's why I don't know how to support 8 Bit characters (or even better UTF-8 or UTF-16).
I'd really like to see this code here at DosTips if you succeeded.
Steffen
Re: Generating .pdf files with batch?
@aGerman:
I made a progress.
1. open NOTEPAD and type
(capital letter U, U with umlaut and again capital letter U).
Execute
and add this below:
So this part looks like this:
Now open this file.pdf and your umlaut letter U is displayed correctly.
PDF reference says that WinAnsiEncoding is CP1252. So this might help you. I don't have all letters covered with this Code Page (I need CP1250). SCARON and ZCARON are OK, CCARON is missing.
(because source is included this can be added)
Saso
I made a progress.
1. open NOTEPAD and type
Code: Select all
UÜU
Execute
Open file.pdf with NOTEPAD and findtext2pdf.exe <file.txt >file.pdf
Code: Select all
/BaseFont /Courier
Code: Select all
/Encoding /WinAnsiEncoding
Code: Select all
/BaseFont /Courier
/Encoding /WinAnsiEncoding
PDF reference says that WinAnsiEncoding is CP1252. So this might help you. I don't have all letters covered with this Code Page (I need CP1250). SCARON and ZCARON are OK, CCARON is missing.
(because source is included this can be added)
Saso
Re: Generating .pdf files with batch?
Interesting!
Steffen
Well, if you do it by your own and you don't publish it then maybe. Otherwise:
I usually respect copyright notes.You may not alter the source in any way other than those mentioned above without the permission of the author
Steffen
Re: Generating .pdf files with batch?
Of course Steffen. It is easier to test this change if .exe is changed.
I keep searching for a solution.
Thanks.
Saso
I keep searching for a solution.
Thanks.
Saso
Last edited by miskox on 18 Dec 2017 06:51, edited 1 time in total.
Re: Generating .pdf files with batch?
I fiddled with the C code for my own. Even if I was able to implement code page 1250 none of the supported monospaced fonts is able to display all of its characters. So I gave up and searched for an alternative. All I found was a tool to convert HTML to PDF though.
Windows (MinGW) from https://wkhtmltopdf.org/downloads.html
You don't need to install it to the program files directory. Freely choose any place. The bin subdirectory contains wkhtmltopdf.exe that seems to work independently of the DLL file.
The workaround for text files is a little clumsy but should work for you
The tool seems to have a bug though. Tabs are reduced to single spaces and the tab-size style property will be completely ignored (I already tried the same in a <pre> element without success).
Steffen
Windows (MinGW) from https://wkhtmltopdf.org/downloads.html
You don't need to install it to the program files directory. Freely choose any place. The bin subdirectory contains wkhtmltopdf.exe that seems to work independently of the DLL file.
The workaround for text files is a little clumsy but should work for you
Code: Select all
@echo off &setlocal DisableDelayedExpansion
set "infile=myfile.txt"
set "outfile=myfile.pdf"
set "codepage=1250"
set "fontname=Courier New"
set "fontsize=15"
for /f "tokens=2 delims=:" %%i in ('chcp') do set /a oemcp=%%~ni
>nul chcp %codepage%
set "html=tmp_%random%.html"
setlocal EnableDelayedExpansion
>"!html!" <"!infile!" (
<nul set /p "=<^!DOCTYPE html><html dir="ltr"><head><meta charset="CP!codepage!"><title></title><style type="text/css">"
<nul set /p "=body {font: !fontsize!px !fontname!; white-space: pre; tab-size: 4; line-height: .75em;}"
<nul set /p "=</style></head><body>"
for /f %%i in ('type "!infile!"^|find /c /v ""') do for /l %%j in (1 1 %%i) do (
set "ln=" &set /p "ln="
if not defined ln (
echo(^<br^>
) else (
set "ln=!ln:&=&!"
set "ln=!ln:<=<!"
set "ln=!ln:>=>!"
echo(!ln!^<br^>
)
)
<nul set /p "=</body></html>"
)
endlocal
:: https://wkhtmltopdf.org/
2>nul wkhtmltopdf "%html%" "%outfile%"
del "%html%"
>nul chcp %oemcp%
pause
Steffen
Re: Generating .pdf files with batch?
Just for fun ... A very basic batch-only solution. Doesn't support multiple pages, tabs, ... Uses a lot of hard-coded defaults. Supports Windows-1252.
text2pdf.bat obsolete ( see viewtopic.php?f=3&t=8289&start=15#p55139 )
Steffen
text2pdf.bat obsolete ( see viewtopic.php?f=3&t=8289&start=15#p55139 )
Code: Select all
@echo off &setlocal DisableDelayedExpansion
set "infile=test.txt"
set "outfile=test.pdf"
for /f %%a in ('copy /z "%~f0" nul') do set "cr=%%a"
for /f "tokens=1,2*" %%a in ('reg query "HKCU\Control Panel\International"^|findstr /i "\<[is]Date\>"') do set "%%a=%%c"
for /f "tokens=1-6 delims=%sDate%:.," %%a in ("%date:* =%,%time: =0%") do (
if %iDate%==0 (set mm=%%a&set dd=%%b&set yy=%%c) else if %iDate%==1 (set dd=%%a&set mm=%%b&set yy=%%c) else (set yy=%%a&set mm=%%b&set dd=%%c)
(set h=%%d&set m=%%e&set s=%%f)
)
if 1%yy% LSS 200 if 1%yy% LSS 170 (set yy=20%yy%) else (set yy=19%yy%)
>"%outfile%" echo(%%PDF-1.1
for %%i in ("%outfile%") do set "objpos[1]=%%~zi"
>>"%outfile%" (
echo(1 0 obj
echo(^<^<
echo(/CreationDate ^(D:%yy%%mm%%dd%%h%%m%%s%^)
echo(/Producer ^(text2pdf.bat^)
echo(/Title ^(text2pdf^)
echo(^>^>
echo(endobj
)
for %%i in ("%outfile%") do set "objpos[2]=%%~zi"
>>"%outfile%" (
echo(2 0 obj
echo(^<^<
echo(/Type /Catalog
echo(/Pages 3 0 R
echo(^>^>
echo(endobj
)
for %%i in ("%outfile%") do set "objpos[4]=%%~zi"
>>"%outfile%" (
echo(4 0 obj
echo(^<^<
echo(/Type /Font
echo(/Subtype /Type1
echo(/Name /F1
echo(/BaseFont /Courier
echo(/Encoding /WinAnsiEncoding
echo(^>^>
echo(endobj
)
for %%i in ("%outfile%") do set "objpos[5]=%%~zi"
>>"%outfile%" (
echo(5 0 obj
echo(^<^<
echo(/Font ^<^< /F1 4 0 R ^>^>
echo(/ProcSet [ /PDF /Text ]
echo(^>^>
echo(endobj
)
for %%i in ("%outfile%") do set "objpos[6]=%%~zi"
>>"%outfile%" (
echo(6 0 obj
echo(^<^<
echo(/Type /Page
echo(/Parent 3 0 R
echo(/Resources 5 0 R
echo(/Contents 7 0 R
echo(^>^>
echo(endobj
)
for %%i in ("%outfile%") do set "objpos[7]=%%~zi"
>>"%outfile%" (
echo(7 0 obj
echo(^<^<
echo(/Length 8 0 R
echo(^>^>
echo(stream
)
for %%i in ("%outfile%") do set "streampos=%%~zi"
>>"%outfile%" (
echo(BT
echo(/F1 10 Tf
echo(1 0 0 1 50 752 Tm
echo(12 TL
)
setlocal EnableDelayedExpansion
<"!infile!" >>"!outfile!" (
for /f %%i in ('type "!infile!"^|find /c /v ""') do for /l %%j in (1 1 %%i) do (
set "ln=" &set /p "ln="
if defined ln (
set "ln=!ln:\=\\!"
set "ln=!ln:(=\(!"
set "ln=!ln:)=\)!"
)
echo(^(!ln!^)'
)
echo(^(^)'
)
endlocal
>>"%outfile%" echo(ET
for %%i in ("%outfile%") do set /a "streamlen=%%~zi-streampos"
>>"%outfile%" (
echo(endstream
echo(endobj
)
for %%i in ("%outfile%") do set "objpos[8]=%%~zi"
>>"%outfile%" (
echo(8 0 obj
echo(%streamlen%
echo(endobj
)
for %%i in ("%outfile%") do set "objpos[3]=%%~zi"
>>"%outfile%" (
echo(3 0 obj
echo(^<^<
echo(/Type /Pages
echo(/Count 1
echo(/MediaBox [ 0 0 612 792 ]
echo(/Kids [ 6 0 R ]
echo(^>^>
echo(endobj
)
for %%i in ("%outfile%") do set "xrefpos=%%~zi"
setlocal EnableDelayedExpansion
>>"!outfile!" (
echo(xref
echo(0 9
<nul set /p "=0000000000 65535 f !cr!"
for /l %%i in (1 1 8) do (
set "pos=0000000000!objpos[%%i]!"
<nul set /p "=!pos:~-10! 00000 n !cr!"
)
echo(trailer
echo(^<^<
echo(/Size 9
echo(/Root 2 0 R
echo(/Info 1 0 R
echo(^>^>
echo(startxref
echo(!xrefpos!
echo(%%%%EOF
echo(
)
endlocal
Re: Generating .pdf files with batch?
Great Steffen!
This is a start.
Saso
(still searching for solutions: embedding font...)
This is a start.
Saso
(still searching for solutions: embedding font...)
Re: Generating .pdf files with batch?
Hello Saso!
The HTML to PDF solution I posted above should work for you. The font can be specified.
Steffen
The HTML to PDF solution I posted above should work for you. The font can be specified.
Steffen
Re: Generating .pdf files with batch?
I get the following error:
With ECHO ON:
I changed delims to:
and now there is no error.
Still doing some tests.
Thanks.
Saso
Code: Select all
:.," was unexpected at this time.
Code: Select all
c:\>set "sDate=. " <----- note a space after the dot.
c:\>set "iDate=1"
c:\>for /F "tokens=1-6 delims=. :.," %a in ("18. 12. 2017,13:36:29,01") do (
if 1 == 0 (set mm=%a & set dd=%b & set yy=%c ) else if 1 == 1 (set dd=%a & set mm=%b & set yy=%c ) else (set yy=%a & set mm=%b & set dd=%c )
(set h=%d & set m=%e & set s=%f )
)
:.," was unexpected at this time.
c:\>
I changed delims to:
Code: Select all
"tokens=1-6 delims=:;,%sDate%"
Still doing some tests.
Thanks.
Saso