Generating .pdf files with batch?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
miskox
Posts: 630
Joined: 28 Jun 2010 03:46

Generating .pdf files with batch?

#1 Post by miskox » 15 Dec 2017 06:09

Hello!

Anyone has ever tried to generate .pdf files with batch? I would really like to generate report files...

Thanks.
Saso

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Generating .pdf files with batch?

#2 Post by aGerman » 15 Dec 2017 06:16

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

miskox
Posts: 630
Joined: 28 Jun 2010 03:46

Re: Generating .pdf files with batch?

#3 Post by miskox » 15 Dec 2017 06:25

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):

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.
- maybe based on that it would be possible to see what is in .pdf file (binary structure).

Thanks.

Saso

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Generating .pdf files with batch?

#4 Post by aGerman » 15 Dec 2017 07:25

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".

Code: Select all

text2pdf "myfile.txt" > "myfile.pdf"
That would work, too:

Code: Select all

(
  echo Hello
  echo World
)|text2pdf > "myfile.pdf"
Steffen

PS: Checked on VirusTotal
https://www.virustotal.com/en/file/c71c ... /analysis/

PPS: Works with ASCII only. No characters >7 Bits supported.

miskox
Posts: 630
Joined: 28 Jun 2010 03:46

Re: Generating .pdf files with batch?

#5 Post by miskox » 15 Dec 2017 11:57

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

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Generating .pdf files with batch?

#6 Post by aGerman » 15 Dec 2017 12:14

miskox wrote:
15 Dec 2017 11:57
.C source is also on the site. Problems might be 8-bit characters (national characters).
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).
miskox wrote:
15 Dec 2017 11:57
So porting it to .cmd is next...
I'd really like to see this code here at DosTips if you succeeded.

Steffen

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Generating .pdf files with batch?

#7 Post by Squashman » 15 Dec 2017 12:36

This isn't a pure batch solution but years ago I used a combination of a program named PrintFile and I also installed a PDF Print Driver. I have used a couple of different ones over the years but I eventually settled on DoPDF.

This worked well for the automation capabilities we needed at the time.

miskox
Posts: 630
Joined: 28 Jun 2010 03:46

Re: Generating .pdf files with batch?

#8 Post by miskox » 15 Dec 2017 13:46

@aGerman:

I made a progress.

1. open NOTEPAD and type

Code: Select all

UÜU
(capital letter U, U with umlaut and again capital letter U).

Execute
text2pdf.exe <file.txt >file.pdf
Open file.pdf with NOTEPAD and find

Code: Select all

/BaseFont /Courier
and add this below:

Code: Select all

/Encoding /WinAnsiEncoding
So this part looks like this:

Code: Select all

/BaseFont /Courier
/Encoding /WinAnsiEncoding
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

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Generating .pdf files with batch?

#9 Post by aGerman » 15 Dec 2017 14:24

Interesting!
miskox wrote:
15 Dec 2017 13:46
(because source is included this can be added)
Well, if you do it by your own and you don't publish it then maybe. Otherwise:
You may not alter the source in any way other than those mentioned above without the permission of the author
I usually respect copyright notes.

Steffen

miskox
Posts: 630
Joined: 28 Jun 2010 03:46

Re: Generating .pdf files with batch?

#10 Post by miskox » 16 Dec 2017 11:31

Of course Steffen. It is easier to test this change if .exe is changed.

I keep searching for a solution.

Thanks.
Saso
Last edited by miskox on 18 Dec 2017 06:51, edited 1 time in total.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Generating .pdf files with batch?

#11 Post by aGerman » 16 Dec 2017 16:11

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

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:&=&amp;!"
      set "ln=!ln:<=&lt;!"
      set "ln=!ln:>=&gt;!"
      echo(!ln!^<br^>
    )
  )

  <nul set /p "=</body></html>"
)
endlocal

:: https://wkhtmltopdf.org/
2>nul wkhtmltopdf "%html%" "%outfile%"

del "%html%"
>nul chcp %oemcp%
pause
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

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Generating .pdf files with batch?

#12 Post by aGerman » 17 Dec 2017 17:44

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 )

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
Steffen

miskox
Posts: 630
Joined: 28 Jun 2010 03:46

Re: Generating .pdf files with batch?

#13 Post by miskox » 18 Dec 2017 01:26

Great Steffen!

This is a start.

Saso

(still searching for solutions: embedding font...)

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Generating .pdf files with batch?

#14 Post by aGerman » 18 Dec 2017 01:40

Hello Saso!

The HTML to PDF solution I posted above should work for you. The font can be specified.

Steffen

miskox
Posts: 630
Joined: 28 Jun 2010 03:46

Re: Generating .pdf files with batch?

#15 Post by miskox » 18 Dec 2017 06:48

I get the following error:

Code: Select all

:.," was unexpected at this time.
With ECHO ON:

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%"
and now there is no error.

Still doing some tests.

Thanks.
Saso

Post Reply