Powershell使用itext.html2pdf将html文件转换为pdf

时间:2019-06-05 22:29:45

标签: powershell-4.0 itext7

如何使用itext.html2pdf在Powershell中将html转换为pdf文件?

我只想获取我的input.html文件并获得一个output.pdf文件

我正在使用iText 7 pdfHTML版本2.1.3

这是itext网站上的C#代码,但是如何将其转换为Powershell?

static void Main(string[] args)
{
 using (FileStream htmlSource = File.Open("input.html", FileMode.Open))
 using (FileStream pdfDest = File.Open("output.pdf", FileMode.OpenOrCreate))
 {
   ConverterProperties converterProperties = new ConverterProperties();
   HtmlConverter.ConvertToPdf(htmlSource, pdfDest, converterProperties);
 }
}

预先感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

请确保将.NET环境的以下依赖项中的二进制文件解压缩到工作目录中:

https://www.nuget.org/packages/BouncyCastle/1.8.1 https://www.nuget.org/packages/itext7.pdfhtml/ https://www.nuget.org/packages/itext7/ https://www.nuget.org/packages/Common.Logging/ https://www.nuget.org/packages/Common.Logging.Core/

然后,使用以下PowerShell代码:

Add-Type -Path "D:\temp\BouncyCastle.Crypto.dll"
Add-Type -Path "D:\temp\Common.Logging.Core.dll"
Add-Type -Path "D:\temp\Common.Logging.dll"
Add-Type -Path "D:\temp\itext.io.dll"
Add-Type -Path "D:\temp\itext.kernel.dll"
Add-Type -Path "D:\temp\itext.forms.dll"
Add-Type -Path "D:\temp\itext.layout.dll"
Add-Type -Path "D:\temp\itext.styledxmlparser.dll"
Add-Type -Path "D:\temp\itext.svg.dll"
Add-Type -Path "D:\temp\itext.html2pdf.dll"


$source = [System.IO.FileInfo]::new("D:\temp\input.html")
$dest = [System.IO.FileInfo]::new("D:\temp\output.pdf")
[iText.Html2Pdf.HtmlConverter]::ConvertToPdf($source, $dest)
相关问题