繁体   English   中英

从 powershell 生成 pdf

[英]Generate a pdf from powershell

我正在尝试使用 powershell 生成 PDF,但我不知道如何继续。 我已经尝试使用 Itext 7 但我不知道如何让它工作。

当我尝试在 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.

我能帮忙吗?

提前致谢

下面是 PowerShell 脚本的代码,它输出带有“Hello World”的 PDF。 写在上面。 它反映了 iText 7 基本 Hello World 示例的功能。 您可以根据您的要求更改它。

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()

PowerShell 依赖项的组合可能会有问题,因为它们需要及时属于已知的工作组,并且 7.1.14 被吹捧为轻量级解决方案,因此请参阅稍后的 TL;DR 编辑或下面的其他评论,并以管理员身份运行可能与 a普通用户。 因此,请仔细执行这些步骤,因为有些步骤可能会降低您当前的设置。

最重要的是使用项目目录并注意您的提示位于该文件夹中,以确保您没有在默认的 PowerShell 目录中运行。 我使用目标目录为“空白/空”的快捷方式,因此默认为当前工作文件夹。

第一次检查:-

project folder>[Net.ServicePointManager]::SecurityProtocol

应该返回 Tls12 或 Tls13 我们需要它是 1.2 所以请记下你的是否设置为 Tls13 并运行此行。

 [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

我们可能需要更改 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

如果没有,您可以将其添加为

Register-PackageSource nuget.org https://www.nuget.org/api/v2/ -ProviderName NuGet

现在您应该能够按如下方式安装 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

仔细检查您的文件夹结构是否正确在此处输入图像描述

请注意脚本的顺序和位置对于正确加载至关重要

    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()

这将产生一个空文件,但证明一切正常,您可以开始运行其他示例,例如 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()

... 祝你好运。

  • 上面的版本是针对用户博客验证 7.1 混音没有太多冲突的固定时间点,目的是生成一组在Net40环境中工作的独立文件,但时间在推移,您应该确保您使用的是较新的组合。 然而,在 7.1.15 中一切都发生了变化,因为 Net 4.5 和现在的 4.6.1 现在需要一个非常大的依赖项列表,尽管 packages/itext7/7.2.1 本身仍然适用于 packages/Portable.BouncyCastle/1.8.9 + 和 common日志记录仍然是 3.4.1

我在这里找到了一些很好的信息,哪些 DLL 需要通过添加类型加载... https://renenyffenegger.ch/notes/design/graphic/pdf/tools/iText/index

通过反复试验,我发现加载以下适用于 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 }
}

只是我的 2 美分,但上面的代码不适用于 Itext7 7.2.1(修改正确的路径后)。
希望我上周看到这篇文章 - 浪费了几天的大部分时间,因为 7.2.1 本身没有表现。 :(

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM