Solved

TXT to PDF help needed

Posted on 2016-08-03
26
83 Views
Last Modified: 2016-08-23
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?
0
Comment
Question by:Mark Wood
  • 13
  • 5
  • 4
  • +2
26 Comments
 
LVL 12

Assisted Solution

by:Benjamin Voglar
Benjamin Voglar earned 250 total points
ID: 41740622
Huuuh, I did it :)

You must have Word installed on the computer.

Try Powershell:

Open Powershell ISE as admin. Past the copy, edit the path and wruuum.

$Files=GET-CHILDITEM 'C:\it\*.txt*'
$Word=NEW-OBJECT –COMOBJECT WORD.APPLICATION

Foreach ($File in $Files) {

    # open a Word document, filename from the directory

    $Doc=$Word.Documents.Open($File.fullname)

    # Swap out DOCX with PDF in the Filename

    $Name=$Doc.Fullname -replace('txt([x]{0,1})',"pdf")

    # Save this File as a PDF in Word 2010/2013
    $Doc.saveas([ref] $Name, [ref] 17)
    $Doc.close()

}

Open in new window

0
 
LVL 2

Author Comment

by:Mark Wood
ID: 41740684
$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.
0
 
LVL 2

Author Comment

by:Mark Wood
ID: 41740688
the above is what came out using a batch file
0
 
LVL 2

Author Comment

by:Mark Wood
ID: 41740697
does work in powershell
0
 
LVL 2

Author Comment

by:Mark Wood
ID: 41740719
Here is a copy of my batch file in txt format.
Copy-Reports.txt
0
 
LVL 1

Expert Comment

by:csePixelated
ID: 41740840
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.
0
 
LVL 1

Expert Comment

by:csePixelated
ID: 41740844
0
 
LVL 2

Author Comment

by:Mark Wood
ID: 41740866
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.
0
 
LVL 2

Author Comment

by:Mark Wood
ID: 41740870
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.
0
 
LVL 2

Author Comment

by:Mark Wood
ID: 41740882
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.
0
 
LVL 1

Expert Comment

by:csePixelated
ID: 41740892
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
0
 
LVL 2

Author Comment

by:Mark Wood
ID: 41740904
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.
0
 
LVL 51

Expert Comment

by:Bill Prew
ID: 41740907
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
0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 51

Expert Comment

by:Bill Prew
ID: 41740914
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
0
 
LVL 2

Author Comment

by:Mark Wood
ID: 41740915
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
0
 
LVL 1

Expert Comment

by:csePixelated
ID: 41740926
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
0
 
LVL 2

Author Comment

by:Mark Wood
ID: 41740982
"winword.exe" "C:\myfolder\*.txt" /mFilePrintDefault  doesn't work. it wants me to specify the name of the text file.
0
 
LVL 51

Expert Comment

by:Joe Winograd, EE MVE
ID: 41741051
> 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
1
 
LVL 2

Author Comment

by:Mark Wood
ID: 41741147
Thanks for the input Joe Winograd. My problem is that I can't install any third party software.
0
 
LVL 1

Expert Comment

by:csePixelated
ID: 41741194
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
0
 
LVL 51

Expert Comment

by:Joe Winograd, EE MVE
ID: 41741396
> 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
0
 
LVL 2

Author Comment

by:Mark Wood
ID: 41742301
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.
0
 
LVL 51

Expert Comment

by:Joe Winograd, EE MVE
ID: 41742813
> 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
0
 
LVL 51

Accepted Solution

by:
Bill Prew earned 250 total points
ID: 41743006
Okay, in the interest of getting a solution that works, here is a VBS based approach.  I created a small VBS script that will process a specified folder, opening each TXT file and savinf as a PDF version.  Tested here and it works.  I then adjusted your BAT script to call this when needed.  Let me know what you think.

txt2pdf.vbs
' Set up filesystem object for usage
Set objFSO = CreateObject("Scripting.FileSystemObject")

' Get folder name to process from command line
If (WScript.Arguments.Count > 0) Then
    strFolder = objFSO.GetAbsolutePathname(WScript.Arguments(0))
    If Not objFSO.FolderExists(strFolder) Then
        WScript.StdErr.WriteLine "*ERROR* - Specified folder [" & strFolder & "] does not exist."
        WScript.Quit
    End If
Else
    WScript.StdErr.WriteLine "*ERROR* - No folder name specified to process."
    WScript.Quit
End If

' Start Word so that we can convert files
Set objWord = CreateObject("Word.Application")
objWord.Visible = False

' Process all TXT files in this folder, convert to PDF
For Each objFile In objFSO.GetFolder(strFolder).Files
   ' Select only the TXT files
   If LCase(objFSO.GetExtensionName(objFile.Name)) = "txt" Then
      ' Open in Word
      Set objDoc = objWord.Documents.Open(objFile.Path)
      ' SaveAs a PDF file
      objWord.ActiveDocument.SaveAs Replace(objFile.Path, ".txt", ".pdf"), 17
      ' Close the file
      objDoc.Close
   End If
Next

' Shut down Word now
objWord.Quit

Open in new window

copy-reports.bat
@echo off

REM;----------------------------------------------------------------------------------------------------
REM;this code sets the variables
REM;----------------------------------------------------------------------------------------------------
Set PrinterName=PDF
set yearfoldername=%date:~-4%
set month=%date:~-10,2%

if "%month%" EQU "02" set asd=January
if "%month%" EQU "03" set asd=February
if "%month%" EQU "04" set asd=March
if "%month%" EQU "05" set asd=April
if "%month%" EQU "06" set asd=May
if "%month%" EQU "07" set asd=June
if "%month%" EQU "08" set asd=July
if "%month%" EQU "09" set asd=August
if "%month%" EQU "10" set asd=September
if "%month%" EQU "11" set asd=October
if "%month%" EQU "12" set asd=November
if "%month%" EQU "01" set asd=December


REM;----------------------------------------------------------------------------------------------------
REM;the code below creates the folder for the current year (YYYY) if it doesn't exist
REM;----------------------------------------------------------------------------------------------------
if not exist M:\Managers\Monthly_Reports\%yearfoldername%  mkdir M:\Managers\Monthly_Reports\%yearfoldername%

REM;----------------------------------------------------------------------------------------------------
REM;the code below creates the folder for the current month (MMMM) if it doesn't exist
REM;----------------------------------------------------------------------------------------------------
if not exist M:\Managers\Monthly_Reports\%yearfoldername%\%asd%  mkdir M:\Managers\Monthly_Reports\%yearfoldername%\%asd%

REM;----------------------------------------------------------------------------------------------------
REM;the code below creates the commanders folder for the current month if it doesn't exist
REM;----------------------------------------------------------------------------------------------------
if not exist M:\Managers\Monthly_Reports\%yearfoldername%\%asd%\Commander  mkdir M:\Managers\Monthly_Reports\%yearfoldername%\%asd%\Commander

REM;----------------------------------------------------------------------------------------------------
REM;the code below creates the HQ_Reserve folder for the current month if it doesn't exist
REM;----------------------------------------------------------------------------------------------------
if not exist M:\Managers\Monthly_Reports\%yearfoldername%\%asd%\HQ_Reserve  mkdir M:\Managers\Monthly_Reports\%yearfoldername%\%asd%\HQ_Reserve

REM;----------------------------------------------------------------------------------------------------
REM;the code below creates the upload_to_portal folder for the current month if it doesn't exist
REM;----------------------------------------------------------------------------------------------------
if not exist M:\Managers\Monthly_Reports\%yearfoldername%\%asd%\Upload_To_Portal  mkdir M:\Managers\Monthly_Reports\%yearfoldername%\%asd%\Upload_To_Portal

REM;----------------------------------------------------------------------------------------------------
REM;the code below logs into the samba share to get the report data
REM;----------------------------------------------------------------------------------------------------   
NET USE P:	\\IPADDRESS\Monthly_Reports	PASSWORD	 /user:USERNAME	 /persistent:yes

REM;----------------------------------------------------------------------------------------------------
REM;the code below copies all of the data files to the current months folder 
REM;---------------------------------------------------------------------------------------------------- 
rem Copy P:\output\*.txt M:\Managers\Monthly_Reports\%yearfoldername%\%asd%

REM;----------------------------------------------------------------------------------------------------
REM;the code below copies the data files to the commanders folder and convert to pdf
REM;---------------------------------------------------------------------------------------------------- 
Copy M:\Managers\Monthly_Reports\%yearfoldername%\%asd%\all60*i.txt M:\Managers\Monthly_Reports\%yearfoldername%\%asd%\Commander
Copy M:\Managers\Monthly_Reports\%yearfoldername%\%asd%\ang60*f.txt M:\Managers\Monthly_Reports\%yearfoldername%\%asd%\Commander
Copy M:\Managers\Monthly_Reports\%yearfoldername%\%asd%\arng60*f.txt M:\Managers\Monthly_Reports\%yearfoldername%\%asd%\Commander
Copy M:\Managers\Monthly_Reports\%yearfoldername%\%asd%\usa60*f.txt M:\Managers\Monthly_Reports\%yearfoldername%\%asd%\Commander
Copy M:\Managers\Monthly_Reports\%yearfoldername%\%asd%\usaf60*f.txt M:\Managers\Monthly_Reports\%yearfoldername%\%asd%\Commander
Copy M:\Managers\Monthly_Reports\USA_All.xls M:\Managers\Monthly_Reports\%yearfoldername%\%asd%\Commander

REN M:\Managers\Monthly_Reports\%yearfoldername%\%asd%\Commander\USA_All.xls USA_%asd%_%yearfoldername%.xls

REM;----------------------------------------------------------------------------------------------------
REM;convert all TXT files to PDF files
REM;----------------------------------------------------------------------------------------------------
cscript txt2pdf.vbs "M:\Managers\Monthly_Reports\%yearfoldername%\%asd%\Commander"

REM;----------------------------------------------------------------------------------------------------
REM;the code below copies the data files to the HQ_Reserve folder and convert to pdf
REM;----------------------------------------------------------------------------------------------------
Copy M:\Managers\Monthly_Reports\%yearfoldername%\%asd%\afrc60*.txt M:\Managers\Monthly_Reports\%yearfoldername%\%asd%\HQ_Reserve
Copy M:\Managers\Monthly_Reports\%yearfoldername%\%asd%\all60*.txt M:\Managers\Monthly_Reports\%yearfoldername%\%asd%\HQ_Reserve
Copy M:\Managers\Monthly_Reports\%yearfoldername%\%asd%\all60*a.txt M:\Managers\Monthly_Reports\%yearfoldername%\%asd%\HQ_Reserve
Copy M:\Managers\Monthly_Reports\%yearfoldername%\%asd%\all60*b.txt M:\Managers\Monthly_Reports\%yearfoldername%\%asd%\HQ_Reserve
Copy M:\Managers\Monthly_Reports\%yearfoldername%\%asd%\all60*c.txt M:\Managers\Monthly_Reports\%yearfoldername%\%asd%\HQ_Reserve
Copy M:\Managers\Monthly_Reports\%yearfoldername%\%asd%\all60*d.txt M:\Managers\Monthly_Reports\%yearfoldername%\%asd%\HQ_Reserve
Copy M:\Managers\Monthly_Reports\%yearfoldername%\%asd%\all60*e.txt M:\Managers\Monthly_Reports\%yearfoldername%\%asd%\HQ_Reserve
Copy M:\Managers\Monthly_Reports\%yearfoldername%\%asd%\all60*f.txt M:\Managers\Monthly_Reports\%yearfoldername%\%asd%\HQ_Reserve
Copy M:\Managers\Monthly_Reports\%yearfoldername%\%asd%\all60*g.txt M:\Managers\Monthly_Reports\%yearfoldername%\%asd%\HQ_Reserve
Copy M:\Managers\Monthly_Reports\%yearfoldername%\%asd%\all60*h.txt M:\Managers\Monthly_Reports\%yearfoldername%\%asd%\HQ_Reserve
Copy M:\Managers\Monthly_Reports\%yearfoldername%\%asd%\all60*i.txt M:\Managers\Monthly_Reports\%yearfoldername%\%asd%\HQ_Reserve
Copy M:\Managers\Monthly_Reports\DISCREP*.txt M:\Managers\Monthly_Reports\%yearfoldername%\%asd%\HQ_Reserve
Copy M:\Managers\Monthly_Reports\total60*.txt M:\Managers\Monthly_Reports\%yearfoldername%\%asd%\HQ_Reserve

REM;----------------------------------------------------------------------------------------------------
REM;the code below copies the data files to the Upload_To_Portal folder and convert to pdf
REM;----------------------------------------------------------------------------------------------------
Copy M:\Managers\Monthly_Reports\%yearfoldername%\%asd%\afrc60*.txt M:\Managers\Monthly_Reports\%yearfoldername%\%asd%\Upload_To_Portal
Copy M:\Managers\Monthly_Reports\%yearfoldername%\%asd%\all60*.txt M:\Managers\Monthly_Reports\%yearfoldername%\%asd%\Upload_To_Portal
Copy M:\Managers\Monthly_Reports\%yearfoldername%\%asd%\all60*a.txt M:\Managers\Monthly_Reports\%yearfoldername%\%asd%\Upload_To_Portal
Copy M:\Managers\Monthly_Reports\%yearfoldername%\%asd%\all60*b.txt M:\Managers\Monthly_Reports\%yearfoldername%\%asd%\Upload_To_Portal
Copy M:\Managers\Monthly_Reports\%yearfoldername%\%asd%\all60*c.txt M:\Managers\Monthly_Reports\%yearfoldername%\%asd%\Upload_To_Portal
Copy M:\Managers\Monthly_Reports\%yearfoldername%\%asd%\all60*d.txt M:\Managers\Monthly_Reports\%yearfoldername%\%asd%\Upload_To_Portal
Copy M:\Managers\Monthly_Reports\%yearfoldername%\%asd%\all60*e.txt M:\Managers\Monthly_Reports\%yearfoldername%\%asd%\Upload_To_Portal
Copy M:\Managers\Monthly_Reports\%yearfoldername%\%asd%\all60*f.txt M:\Managers\Monthly_Reports\%yearfoldername%\%asd%\Upload_To_Portal
Copy M:\Managers\Monthly_Reports\%yearfoldername%\%asd%\all60*g.txt M:\Managers\Monthly_Reports\%yearfoldername%\%asd%\Upload_To_Portal
Copy M:\Managers\Monthly_Reports\%yearfoldername%\%asd%\all60*h.txt M:\Managers\Monthly_Reports\%yearfoldername%\%asd%\Upload_To_Portal
Copy M:\Managers\Monthly_Reports\%yearfoldername%\%asd%\all60*i.txt M:\Managers\Monthly_Reports\%yearfoldername%\%asd%\Upload_To_Portal
Copy M:\Managers\Monthly_Reports\DISCREP*.txt M:\Managers\Monthly_Reports\%yearfoldername%\%asd%\Upload_To_Portal
Copy M:\Managers\Monthly_Reports\total60*.txt M:\Managers\Monthly_Reports\%yearfoldername%\%asd%\Upload_To_Portal
Copy M:\Managers\Monthly_Reports\TJAG*.txt M:\Managers\Monthly_Reports\%yearfoldername%\%asd%\Upload_To_Portal


del M:\Managers\Monthly_Reports\%yearfoldername%\%asd%\*.txt
del M:\Managers\Monthly_Reports\*.txt

NET USE P: /d

Open in new window

~bp
1
 
LVL 2

Author Closing Comment

by:Mark Wood
ID: 41766992
Thanks so much guys. Sorry for the delayed response but I have had to deal with some personal issues.
0
 
LVL 51

Expert Comment

by:Joe Winograd, EE MVE
ID: 41767019
You're welcome, Mark. Happy to help. Hope your personal issues get resolved. Regards, Joe
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

"Migrate" an SMTP relay receive connector to a new server using info from an old server.
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
In this video, we show how to convert an image-only PDF file into a PDF Searchable Image file, that is, a file with both the image (typically from scanning) and text, which is created in an automated fashion with Optical Character Recognition (OCR) …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now