Generating .pdf files with batch?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Generating .pdf files with batch?

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

Hello Saso

I actually referred to the post before :? Did you already try it?

As to my batch solution ... To be honest I only chose that awkward way to create a time stamp because I can't remember when Microsoft introduced the WMIC command.

Code: Select all

for /f "delims=." %%a in ('WMIC OS Get LocalDateTime /value') do for /f %%b in ("%%a") do set "%%b"

...

  echo(/CreationDate ^(D:%LocalDateTime%^)
...would have been easier.
I made some progress. I found a way to replace tabs with spaces. But I have to process strings that contain tabs character-wise (which takes ages) because the number of spaces does always depend on the length of the substring before. Since the script contains a tab character I cant paste it in a code box anymore. The forum software would destroy it.
obsolete
txt2pdf.zip
(1.41 KiB) Downloaded 408 times
Next step is supporting multiple pages which shouldn't be that hard. But I'm afraid supporting code page 1250 would be something that I'm not able to do.

Steffen

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

Re: Generating .pdf files with batch?

#17 Post by aGerman » 18 Dec 2017 17:36

Okay, next version that supports multiple pages, customized font size and landscape orientation. Too long lines will not be wrapped though. Implemting that would mess up the script even more.

Steffen

obsolete
Attachments
txt2pdf.zip
(1.88 KiB) Downloaded 417 times

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

Re: Generating .pdf files with batch?

#18 Post by Squashman » 18 Dec 2017 20:09

aGerman wrote:
18 Dec 2017 17:36
Okay, next version that supports multiple pages, customized font size and landscape orientation. Too long lines will not be wrapped though. Implemting that would mess up the script even more.

Steffen
This has to be the coolest bat file on the planet.

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

Re: Generating .pdf files with batch?

#19 Post by aGerman » 19 Dec 2017 01:15

Thanks Squashman :) The C code of Phil Smith was a pretty good template. I still don't understand much of the structure of PDF.
A batch solution will never get as useful as converters written in other languages because it's just much too slow. But it would help if someone knows of a better way to replace tabs with a calculated number of spaces. Walking through the string character-wise is terribly slow.

Steffen

elzooilogico
Posts: 128
Joined: 23 May 2016 15:39
Location: Spain

Re: Generating .pdf files with batch?

#20 Post by elzooilogico » 19 Dec 2017 03:28

Just a note about viewtopic.php?p=55088#p55088

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:\>
If I remember right, the space must be the last character after delims keyword. So the line

Code: Select all

for /F "tokens=1-6 delims=. :.," %a in ("18. 12. 2017,13:36:29,01") do (
should be

Code: Select all

for /F "tokens=1-6 delims=.:., " %a in ("18. 12. 2017,13:36:29,01") do (

penpen
Expert
Posts: 1991
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Generating .pdf files with batch?

#21 Post by penpen » 19 Dec 2017 07:29

aGerman wrote:
18 Dec 2017 11:15
Since the script contains a tab character I cant paste it in a code box anymore. The forum software would destroy it
You could use the follwing to create a tab character:

Code: Select all

@echo off
setlocal enableExtensions enableDelayedExpansion

:: ÿþ <- needed for not autoconverting to utf-8 in notepad.exe
:: Assumed output: ANSI.
:: Assumed notepad.exe codepage: 1252.
set "cp=850"
for /f "tokens=2 delims=:." %%a in ('chcp') do set "cp=%%~a"
>nul chcp 65001
for /f "delims=" %%a in ('cmd /d /u /cecho ĉ') do set "tab=%%~a"
>nul chcp 850
set "tab=%tab:~0,1%"
>nul chcp %cp%

set "tabWidth=8"
for /l %%a in (1, 1, %tabWidth%) do set "tabs=%tab%"

echo a%tab%b
echo aa%tab%b
echo aaa%tab%b

endlocal
goto :eof
penpen

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

Re: Generating .pdf files with batch?

#22 Post by aGerman » 19 Dec 2017 07:59

elzooilogico wrote:
19 Dec 2017 03:28
If I remember right, the space must be the last character after delims keyword.
That's correct. In my latest script I used "tokens=1,3" instead of "tokens=1,2*" to automatically eliminate this unexpected space. (The latter would be useful if you expect to have spaces in the data in order to get the entire string.)
penpen wrote:
19 Dec 2017 07:29
:: Assumed notepad.exe codepage: 1252.
I didn't assume that because in case of Saso I already know it better :wink:

Steffen

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

Re: Generating .pdf files with batch?

#23 Post by Squashman » 19 Dec 2017 10:56

Just some quick observations on a File Size.

Text File: 12,977 Bytes
Bat PDF: 15,568
Chrome PDF: 33,739
Word PDF: 72,278

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

Re: Generating .pdf files with batch?

#24 Post by aGerman » 19 Dec 2017 12:37

That can easily be explained Squashman. The batch solution works with font Courier. That's one of only a few fonts that are guaranteed to work by default and it's the only monospaced font out of them.
Browsers and Word have to be able to handle Unicode. That means the characters are either UTF-8 (assumed for Chrome) or UTF-16 (assumed for Word) encoded. Since Courier doesn't even support all characters of different single-byte code pages you can imagine that a different font has to be used. Thus, the font (or at least the used glyphs of the font) have to be included into the pdf document. As far as I read these informations along with the text are compressed using a complicated zip-like algorithm. However, these extra informations make that the file size grows...

Steffen

aisha
Posts: 26
Joined: 28 Sep 2016 06:40

Re: Generating .pdf files with batch?

#25 Post by aisha » 19 Dec 2017 16:13

A few options for you:
free or commercial is Coherent PDF
http://www.coherentpdf.com/
"Coherent PDF Command Line Tools give you a wide range of professional, robust tools to modify PDF files. All based on our own PDF technology"
and they have a 70-page manual http://www.coherentpdf.com/manuals.html

I do not use this one, I run a proprietary closed source app, but last I looked at Coherent it looked pretty good.

Aisha

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

Re: Generating .pdf files with batch?

#26 Post by miskox » 20 Dec 2017 03:39

@Steffen:

Your latest version works on XP but fails on my Windows 10 at work.

Code: Select all

c:\>for /F "tokens=1-6 delims=:.,." %a in ("20. 12. 2017,10:34:39,88") 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 )
)

c:\>(
if 1 == 0 (set mm=20  & set dd= 12  & set yy= 2017 )  else if 1 == 1 (set dd=20  & set mm= 12  & set yy= 2017 )  else (set yy=20  & set mm= 12  & set dd= 2017 )
 (set h=10  & set m=34  & set s=39 )
)

2017 was unexpected at this time.
c:\>if 1 2017 LSS 200 if 1 2017 LSS 170 (set yy=20 2017) else (set yy=19 2017)
As you can see now sDate does not have a space in it so delim= does not contain a space and it fails. The other version had a space in it.

Otherwise it works very well.

Saso

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

Re: Generating .pdf files with batch?

#27 Post by aGerman » 20 Dec 2017 04:30

:shock: I would never have expected to see two characters as date separator, Saso. I'll make a command line tool (with options) out of the code and post it this evening.

Steffen

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

Re: Generating .pdf files with batch?

#28 Post by miskox » 20 Dec 2017 10:24

Steffen!

I think the problem is because (I would guess) there are no official rules how to write a date. We use DD.MM.YYYY.

I read/heared about two explanations how dates should be written.

1. DD.MM.YYYY: a classic way (no spaces between dots and numbers). Leading zeroes are allowed (01.12.2017 for 1-dec-2017).

2. d. m. yyyy.: beacuse a dot is a 'non connecting delimiter in sentences (after a period at the end of the sentence a space must follow and then new word can start a sentence - This is an example.There is no space here before a period - there should be one space. Period.). With such an explanation dates should be written as: 1. 12. 2017 (space after a dot and no leading zeroes).

Saso

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

Re: Generating .pdf files with batch?

#29 Post by Squashman » 20 Dec 2017 10:32

aGerman wrote:
20 Dec 2017 04:30
:shock: I would never have expected to see two characters as date separator,

Steffen
I have used the registry like that in the past as well and I have never see it like that either. I always thought that was a rock solid way to get the date.

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

Re: Generating .pdf files with batch?

#30 Post by aGerman » 20 Dec 2017 11:58

Here it goes...
I made it a tool to work with command line arguments. Processing of %date% and %time% variables (where I rolled back to Saso's suggestion for the token processing) will now only be in effect if WMIC isn't available. Fixed a minor bug for tab discovering. Improved the speed by command line concatenation (labels are found noticeably faster). Disadvantage of the latter is that the code isn't human-readable anymore.

Code: Select all

Convert Text to PDF

Usage:
txt2pdf.bat inFile outFile [/A4|/A3|/Letter] [/T:n] [/F:n] [/L]

  inFile   Path of the source text file.
  outFile  Path of the destination PDF file.

  /A4      Page size A4.          (default)
  /A3      Page size A3.
  /Letter  Page size Letter.

  /T:n     Tab width of n spaces. (default  8)
  /F:n     Font size of n points. (default 10)
  /L       Orientation Landscape. (default Portrait)
Known issues:
- Slow (well, it's Batch) and even slower the more lines contain tab characters.
- Doesn't wrap lines that exceed the page width. (won't fix)
- Supports ASCII and Windows-1252 only. (can't fix)
- Supports font Courier only. (can't fix)

current
Attachments
txt2pdf.zip
(2.69 KiB) Downloaded 427 times

Post Reply