Convert text file to PDF file using Powershell

时光毁灭记忆、已成空白 提交于 2021-01-29 11:28:04

问题


I have a text file that has simple firmware versions on it. I need to convert the text file to a secure pdf file using powershell scripts.

Also, Is there a way to put a date and timestamp on the file when its converted?

I don't quite know how to do it I'm fairly new using powershell, thanks!


回答1:


I got this to work with Word 2010, but it should work with 2007 as well:

# File paths
$txtPath = "C:\Users\Public\test.txt"
$pdfPath = "C:\Users\Public\test.pdf"

# Required Word Variables
$wdExportFormatPDF = 17
$wdDoNotSaveChanges = 0

# Create a hidden Word window
$word = New-Object -ComObject word.application
$word.visible = $false

# Add a Word document
$doc = $word.documents.add()

# Put the text into the Word document
$txt = Get-Content $txtPath
$selection = $word.selection
$selection.typeText($txt)

# Set the page orientation to landscape
$doc.PageSetup.Orientation = 1

# Export the PDF file and close without saving a Word document
$doc.ExportAsFixedFormat($pdfPath,$wdExportFormatPDF)
$doc.close([ref]$wdDoNotSaveChanges)
$word.Quit()



回答2:


An alternative to having to assume that the user has Word or some other such application installed would be to include a library with your script that will support converting text to PDF. iTextSharp was specifically designed to work with .Net to create and maintain PDF documents. You can get it here.

The only stipulation is that it is licensed software using the AGPL (free!) license structure unless you plan on packing this within your own software or some such and don't plan on having it be open source. It sounds like you're just writing a script, that should not be a problem. If you go find a v4 copy of iTextSharp you can actually get away without having to disclose everything as open source, so you may want to go that route.

Good reference for using it with powershell is slipsec.com/blog post 414, which is where I usually reference to borrow code when I have to use the library myself (twice, but I think it's tedious and annoying so I don't use it often, but then again I rarely need to generate PDF documents in bulk or in an automated fashion).

So, the nitty gritty... This loads the DLL, creates the objects needed to generate the PDF file (it's using the font Ariel, at a font size of 10).

Then it reads the source file (C:\temp\source.txt is used as an example), and adds the content to the $paragraph object, adding a New Line character for each line, otherwise is smashes all of the text into one big ugly clump.

Then it opens the file object (effectively creating the file), adds the $paragraph object to the file, and closes the file. No save is needed, adding the $paragraph object writes the data directly to the drive.

[System.Reflection.Assembly]::LoadFrom("C:\path\to\itextsharp\itextsharp.dll")

$doc = New-Object iTextSharp.text.Document
$fileStream = New-Object IO.FileStream("C:\temp\output.pdf", [System.IO.FileMode]::Create)
[iTextSharp.text.pdf.PdfWriter]::GetInstance($doc, $filestream)

#iTextSharp provides a class to work with fonts, but first we have to register them:
[iTextSharp.text.FontFactory]::RegisterDirectories()
$arial = [iTextSharp.text.FontFactory]::GetFont("arial", 10)

$paragraph = New-Object iTextSharp.text.Paragraph

$paragraph.add((gc C:\temp\source.txt|%{"$_`n"})) | Out-Null
$doc.open()
$doc.add($paragraph) | Out-Null
$doc.close()


来源:https://stackoverflow.com/questions/23892631/convert-text-file-to-pdf-file-using-powershell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!