Link to home
Create AccountLog in
Avatar of Mark Wood
Mark WoodFlag for United States of America

asked on

TXT to PDF help needed

I have some text files in a folder that I need to be able to send to the convert to pdfs using the pdf printer on my computer using a batch file.

Can this be done and how?
SOLUTION
Avatar of Benjamin Voglar
Benjamin Voglar
Flag of Slovenia image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of Mark Wood

ASKER

$Files' is not recognized as an internal or external command,
 operable program or batch file.
 '$Word' is not recognized as an internal or external command,
 operable program or batch file.
 'Foreach' is not recognized as an internal or external command,
 operable program or batch file.
 '#' is not recognized as an internal or external command,
 operable program or batch file.
 '$Doc' is not recognized as an internal or external command,
 operable program or batch file.
 '#' is not recognized as an internal or external command,
 operable program or batch file.
 '$Name' is not recognized as an internal or external command,
 operable program or batch file.
 '#' is not recognized as an internal or external command,
 operable program or batch file.
 '$Doc.saveas' is not recognized as an internal or external command,
 operable program or batch file.
 '$Doc.close' is not recognized as an internal or external command,
 operable program or batch file.
 '}' is not recognized as an internal or external command,
 operable program or batch file.
the above is what came out using a batch file
does work in powershell
Here is a copy of my batch file in txt format.
Copy-Reports.txt
Ide try using bullzip, the demo version works well for me.

http://download.cnet.com/BullZip-PDF-Printer-Standard/3000-18497_4-85827.html

or you could buy it from http://www.bullzip.com/

Once installed should just work.
bullzip will not work as this network does not allow for the install of un-approved software and it takes months to get something approved and I have only days.
I have seen http://www.robvanderwoude.com/printfiles.php#PrintPDF and that will print pdfs but what I need to do is convert the txt files to pdf's and not actually print them.
Benjamin Voglar posted a powershell script that works if I edit it and set the path for the text files each time but as you can see in my batch file the path changes from month to month.

If his script worked as a batch file it would be perfect.
So you are looking for a way to convert that does not require an install.
or are you bared from using exe files.
how many do you have, are you willing to use an online utility or do you require it to be batch?

http://www.pdfzilla.com/zilla_txt_to_pdf_converter.html
it needs to work from the batch. I can use exe such as word or acrobat which are already installed. I need to convert like 8 files every time I run the script. all of the txt files are in the same folder.
Avatar of Bill Prew
Bill Prew

You can certainly put the Powershell code into a PS1 and then execute it from a BAT file, like you would another BAT file.  You can even pass it parms when you invoke it.  I added Powershell to the zones of this question, and also sent a message to a frequent Powershell expert, I think with just a little help you can adapt what you have with the powershell code suggested already.

~bp
And for what it's worth, I have looked in the past for a freeware command line driven TXT to PDF converter and never found anything useful.  If you want to spend some money that's a different story though, there are a few GUI tools out there that also can be driven via command line, but they will cost a little to purchase them.  Would that be an option, or rather just automate the Word approach as discussed already?

~bp
Thanks Bill, that may be the solution I am looking for. I just don't know how to change all of what I have already done to work in powershell. I have re-attached a copy of what I have already done in my batch file.
Copy-Reports.txt
I understand some of the limitations from running PS1 files from a computer with restrictions.

If you can set your default printer
ya may try
"winword.exe" "C:\myfolder\*.txt" /mFilePrintDefault
"winword.exe" "C:\myfolder\*.txt" /mFilePrintDefault  doesn't work. it wants me to specify the name of the text file.
> I have looked in the past for a freeware command line driven TXT to PDF converter and never found anything useful.

Hi Bill,
You should give VeryPDF's Text to PDF Converter a try:
http://www.verypdf.com/app/text-to-pdf-converter/index.html

It is excellent — and free for personal use. It has a GUI, but the real power is in its command line interface. The Help file is here (the installer also puts it in the installation folder):
http://www.verypdf.com/txt2pdf/help.htm

Scroll down there and you'll see Section 4 — Command Line Options. In its simplest form, you would run this:

txt2pdf.exe input.txt

Works a charm! Section 6 of the Help says, "Windows98/ME/NT/2000/XP/Vista", but I'm running it on W7/64-bit with no problems. I think it's just that the Help page hasn't been updated. The info/index page is more up-to-date and says, "Windows 98/ME/NT/2000/XP/2003/Vista/7/2008 of both 32-bit and 64-bit".

Hi Mark,
The simple pdf2txt.exe command that I show above will work fine in a batch file running against all text files in a folder. It will create a PDF file with the same file name as the TXT file, but, of course, with the PDF file type. For example, this one-line batch file will do it (assuming the batch file, the text files, and txt2pdf.exe are in the same folder — if not, adjust accordingly):

forfiles /m *.txt /c "cmd /c txt2pdf.exe @path"

Regards, Joe
Thanks for the input Joe Winograd. My problem is that I can't install any third party software.
Mark, all you need is the txt2pdf.exe then adjust your commands to the path you place the exe in

unfortunately. i wont be able to post the exe for you as it could violate experts exchange file.

if you have 7-zip ya may see if ya can direct extract the exe.
or try some of the following
http://betanews.com/2015/12/01/how-to-extract-the-contents-of-a-setup-file-without-installing-it-first/

https://sourceforge.net/projects/innounp/?source=typ_redirect
> My problem is that I can't install any third party software.

Is the issue that you can't install third-party software or can't even use it? If you're allowed to use a no-install, third-party EXE, then install it on a non-company computer and copy the txt2pdf.exe file (which will be in c:\Program Files\Text2PDF v1.5\ or c:\Program Files (x86)\Text2PDF v1.5\) to the company computer. It is a stand-alone EXE, not requiring any of its own DLLs or any other supporting files. Unfortunately, the setup file (verypdf-free-txt2pdf.exe) is not in a standard compressed/archived file format, so unzip utilities (like 7-Zip) can't extract the files.

As a side comment, when you post questions in the future, I recommend that you state up-front that you are not allowed to install (or even use, as the case may be) third-party tools. This will prevent folks from spending a lot of time on a solution that you are unable to deploy. Thanks for that kind consideration. Regards, Joe
Joe, yes that is the case. I cannot use or install third party tools as it is not allowed on the network. I will remember to mention that in the future.

This is a script that I am trying to write for use on a medical network so that the hospital managers can run it and it will give them the reports they need in pdf format.

If there is no other option than to create the pdfs manually, since I do not know the syntax to convert this all to ps1 format, then I so be it.
> does work in powershell
> as you can see in my batch file the path changes from month to month
> If his script worked as a batch file it would be perfect.

Mark,

I am neither a PowerShell nor a batch file expert, but I do know that you can run a PowerShell script in a batch file, as follows:

PowerShell.exe -ExecutionPolicy Bypass -Command "c:\temp\TXTtoPDF.ps1"

To pass it a parameter from a batch file, let's say the param is month and that it is SET in the batch file. So your batch file call to PS would look like this:

PowerShell.exe -ExecutionPolicy Bypass -Command "c:\temp\TXTtoPDF.ps1" %month%

Your PS script with Benjamin's code (that you say works fine — I haven't tested it) would have this line at the top:

param ([string]$month)

And then in the rest of the script you can reference the $month variable. Regards, Joe
ASKER CERTIFIED SOLUTION
Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
Thanks so much guys. Sorry for the delayed response but I have had to deal with some personal issues.
You're welcome, Mark. Happy to help. Hope your personal issues get resolved. Regards, Joe