[英]Generate a pdf from powershell
I'm trying to generate a PDF with powershell but I don't know how to proceed.我正在尝试使用 powershell 生成 PDF,但我不知道如何继续。 I already tried to use Itext 7 but I don't know how to make it work.
我已经尝试使用 Itext 7 但我不知道如何让它工作。
When i try to install Itext7 on powershell i have this error message:当我尝试在 powershell 上安装 Itext7 时出现以下错误消息:
No match found for the specified search criteria and the itext7 package name. Use
Get-PackageSource to display for all available registered package sources.
Could I have some help?我能帮忙吗?
Thanks in advance提前致谢
Below is the code for a PowerShell Script which outputs a PDF with "Hello World."下面是 PowerShell 脚本的代码,它输出带有“Hello World”的 PDF。 written on it.
写在上面。 It mirrors the functionality of iText 7 basic Hello World example.
它反映了 iText 7 基本 Hello World 示例的功能。 You can change it as per your requirement.
您可以根据您的要求更改它。
Add-Type -Path "C:\temp\Common.Logging.Core.dll"
Add-Type -Path "C:\temp\Common.Logging.dll"
Add-Type -Path "C:\temp\BouncyCastle.Crypto.dll"
Add-Type -Path "C:\temp\itext.io.dll"
Add-Type -Path "C:\temp\itext.layout.dll"
Add-Type -Path "C:\temp\itext.kernel.dll"
[string] $DEST = "C:\files\HelloWorldPowerShell.pdf"
$pdfWriter = [iText.Kernel.Pdf.PdfWriter]::new($DEST)
$pdf = [iText.Kernel.Pdf.PdfDocument]::new($pdfWriter)
$document = [iText.Layout.Document]::new($pdf)
$document.Add([iText.Layout.Element.Paragraph]::new("Hello World!"))
$pdf.Close()
The combination of PowerShell dependencies can be problematic as they need to be of a known working group in time and 7.1.14 was touted as a light solution so see later TL;DR edits or others comments below, and run as Admin perhaps different to a normal user. PowerShell 依赖项的组合可能会有问题,因为它们需要及时属于已知的工作组,并且 7.1.14 被吹捧为轻量级解决方案,因此请参阅稍后的 TL;DR 编辑或下面的其他评论,并以管理员身份运行可能与 a普通用户。 So follow these steps carefully as some may downgrade your current settings.
因此,请仔细执行这些步骤,因为有些步骤可能会降低您当前的设置。
MOST IMPORTANT use a project directory and watch that your prompt is located in that folder to ensure you are not running in the default PowerShell directory.最重要的是使用项目目录并注意您的提示位于该文件夹中,以确保您没有在默认的 PowerShell 目录中运行。 I use a shortcut where the target directory is "blank/empty" thus defaults to the current working folder.
我使用目标目录为“空白/空”的快捷方式,因此默认为当前工作文件夹。
1st Check:-第一次检查:-
project folder>[Net.ServicePointManager]::SecurityProtocol
should return either Tls12 or Tls13 we need it to be 1.2 so keep a note if yours is set to Tls13 and run this line.应该返回 Tls12 或 Tls13 我们需要它是 1.2 所以请记下你的是否设置为 Tls13 并运行此行。
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
we may need to possibly change package provider so first check if nuget includes https://www.nuget.org/api/v2/:-我们可能需要更改 package 提供商,因此首先检查 nuget 是否包含https://www.nuget.org/api/v2/:-
> Get-PackageSource
Name ProviderName IsTrusted Location
---- ------------ --------- --------
nuget.org NuGet False https://www.nuget.org/api/v2/
PSGallery PowerShellGet False https://www.powershellgallery.com/api/v2
If not you can add it as如果没有,您可以将其添加为
Register-PackageSource nuget.org https://www.nuget.org/api/v2/ -ProviderName NuGet
now you should be able to install the dlls as follows现在您应该能够按如下方式安装 dll
Install-Package -Name "itext7" -ProviderName NuGet -RequiredVersion 7.1.14 -Destination . -SkipDependencies
Install-Package -Name Portable.BouncyCastle -ProviderName NuGet -RequiredVersion 1.8.9.0 -Destination . -SkipDependencies
Install-Package -Name Common.Logging -ProviderName NuGet -RequiredVersion 3.4.1.0 -Destination . -SkipDependencies
Install-Package -Name Common.Logging.Core -ProviderName NuGet -RequiredVersion 3.4.1.0 -Destination . -SkipDependencies
Double check your folder has the correct structure仔细检查您的文件夹结构是否正确
Note the order and location of the script is critical for correct loading请注意脚本的顺序和位置对于正确加载至关重要
Add-Type -Path (Join-Path $PSScriptRoot ".\Common.Logging.Core.3.4.1\lib\net40\Common.Logging.Core.dll")
Add-Type -Path (Join-Path $PSScriptRoot ".\Common.Logging.3.4.1\lib\net40\Common.Logging.dll")
Add-Type -Path (Join-Path $PSScriptRoot ".\Portable.BouncyCastle.1.8.9\lib\net40\BouncyCastle.Crypto.dll")
Add-Type -Path (Join-Path $PSScriptRoot ".\itext7.7.1.14\lib\net40\itext.io.dll")
Add-Type -Path (Join-Path $PSScriptRoot ".\itext7.7.1.14\lib\net40\itext.layout.dll")
Add-Type -Path (Join-Path $PSScriptRoot ".\itext7.7.1.14\lib\net40\itext.kernel.dll")
$pdfDocuFilename = (join-path $PSScriptRoot "My1st.pdf")
$pdfWriter = [iText.Kernel.Pdf.PdfWriter]::new($pdfDocuFilename)
$pdfdocument = [iText.Kernel.Pdf.PdfDocument]::new($pdfWriter)
$pdfdocument.AddNewPage()
$pdfdocument.Close()
This will produce an empty file but proves all is well, and you can start running other examples such as the one suggested by S_G, so after the loading Add-Type block replace my blank example with这将产生一个空文件,但证明一切正常,您可以开始运行其他示例,例如 S_G 建议的示例,因此在加载 Add-Type 块后,将我的空白示例替换为
[string] $DEST = "HelloWorld.pdf"
$pdfWriter = [iText.Kernel.Pdf.PdfWriter]::new($DEST)
$pdf = [iText.Kernel.Pdf.PdfDocument]::new($pdfWriter)
$document = [iText.Layout.Document]::new($pdf)
$document.Add([iText.Layout.Element.Paragraph]::new("Hello World! from Powershell"))
$pdf.Close()
... Good Luck. ... 祝你好运。
I found some good info here what DLLs need to be loaded via add-type... https://renenyffenegger.ch/notes/design/graphic/pdf/tools/iText/index我在这里找到了一些很好的信息,哪些 DLL 需要通过添加类型加载... https://renenyffenegger.ch/notes/design/graphic/pdf/tools/iText/index
Through trial & error I found loading the below works for itext7 versions 7.2.0, 7.2.4 & 7.2.5.通过反复试验,我发现加载以下适用于 itext7 版本 7.2.0、7.2.4 和 7.2.5。
# DLL list - https://www.nuget.org/packages/itext7/
$dll_list = @(
"$my_ScriptDir\DLL_7.2.4\BouncyCastle.Crypto.dll"
"$my_ScriptDir\DLL_7.2.4\Common.Logging.Core.dll"
"$my_ScriptDir\DLL_7.2.4\Common.Logging.dll"
"$my_ScriptDir\DLL_7.2.4\itext.commons.dll"
"$my_ScriptDir\DLL_7.2.4\itext.forms.dll"
"$my_ScriptDir\DLL_7.2.4\itext.io.dll"
"$my_ScriptDir\DLL_7.2.4\itext.kernel.dll"
"$my_ScriptDir\DLL_7.2.4\itext.layout.dll"
"$my_ScriptDir\DLL_7.2.4\Microsoft.Bcl.AsyncInterfaces.dll"
"$my_ScriptDir\DLL_7.2.4\Microsoft.Extensions.DependencyInjection.Abstractions.dll"
"$my_ScriptDir\DLL_7.2.4\Microsoft.Extensions.DependencyInjection.dll"
"$my_ScriptDir\DLL_7.2.4\Microsoft.Extensions.Logging.Abstractions.dll"
"$my_ScriptDir\DLL_7.2.4\Microsoft.Extensions.Logging.dll"
"$my_ScriptDir\DLL_7.2.4\Microsoft.Extensions.Options.dll"
"$my_ScriptDir\DLL_7.2.4\Microsoft.Extensions.Primitives.dll"
"$my_ScriptDir\DLL_7.2.4\System.Diagnostics.DiagnosticSource.dll"
"$my_ScriptDir\DLL_7.2.4\System.Memory.dll"
"$my_ScriptDir\DLL_7.2.4\System.Runtime.CompilerServices.Unsafe.dll"
"$my_ScriptDir\DLL_7.2.4\System.Threading.Tasks.Extensions.dll"
"$my_ScriptDir\DLL_7.2.4\System.ValueTuple.dll"
"$my_ScriptDir\DLL_7.2.4\Newtonsoft.Json.dll"
)
# Loop & load DLLs
foreach ($dll in $dll_list)
{
Write-Host "Loading $dll" -ForegroundColor Green
try { Add-Type -Path "$dll"}
catch { $dll.Exception.LoaderExceptions }
}
Just my 2 cents but the above code does NOT work with Itext7 7.2.1 (after modifying for proper paths).只是我的 2 美分,但上面的代码不适用于 Itext7 7.2.1(修改正确的路径后)。
Wish I'd seen this post last week - wasted most of several days pulling hair out over 7.2.1 not behaving itself.希望我上周看到这篇文章 - 浪费了几天的大部分时间,因为 7.2.1 本身没有表现。 :(
:(
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.