【问题标题】:Generate a pdf from powershell从 powershell 生成 pdf
【发布时间】:2023-01-30 14:25:29
【问题描述】:

我正在尝试使用 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 pdf itext7


    【解决方案1】:

    PowerShell 依赖项的组合可能会出现问题,因为它们需要是及时知道工作组,7.1.14 被吹捧为轻型解决方案所以请参阅下面的 TL;DR 编辑或其他 cmets,并以可能与普通用户不同的管理员身份运行。因此,请仔细执行这些步骤,因为有些步骤可能会降低您当前的设置。

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

    第一次检查:-

    project folder>[Net.ServicePointManager]::SecurityProtocol
    

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

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

    我们可能需要更改包提供程序,因此首先检查 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.1lib
    et40Common.Logging.Core.dll")
        Add-Type -Path (Join-Path $PSScriptRoot ".Common.Logging.3.4.1lib
    et40Common.Logging.dll")
        Add-Type -Path (Join-Path $PSScriptRoot ".Portable.BouncyCastle.1.8.9lib
    et40BouncyCastle.Crypto.dll")
        Add-Type -Path (Join-Path $PSScriptRoot ".itext7.7.1.14lib
    et40itext.io.dll")
        Add-Type -Path (Join-Path $PSScriptRoot ".itext7.7.1.14lib
    et40itext.layout.dll")
        Add-Type -Path (Join-Path $PSScriptRoot ".itext7.7.1.14lib
    et40itext.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 混音没有太多冲突时,目标是生成一组在其中工作的独立文件网络40环境,但时间在推移,您应该确保您使用的是更新的混音。然而,一切都在 7.1.15 中发生了变化由于现在 Net 4.5 和现在的 4.6.1 需要更多的依赖项列表,尽管 packages/itext7/7.2.1 本身仍然适用 packages/Portable.BouncyCastle/1.8.9 + 和通用日志记录仍然是 3.4.1

    【讨论】:

    • 太感谢了!!!这非常有效。你为我节省了很多时间
    【解决方案2】:

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

    Add-Type -Path "C:	empCommon.Logging.Core.dll"
    Add-Type -Path "C:	empCommon.Logging.dll"
    Add-Type -Path "C:	empBouncyCastle.Crypto.dll"
    Add-Type -Path "C:	empitext.io.dll"
    Add-Type -Path "C:	empitext.layout.dll"
    Add-Type -Path "C:	empitext.kernel.dll"
    
    [string] $DEST = "C:ilesHelloWorldPowerShell.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()
    

    【讨论】:

    • 谢谢,但我如何安装指定的 dll?
    • 请检查此 nuget.org/packages/itext7 从包管理器运行以下命令以下载 iText 7 dll 文件 Install-Package itext7 -Version 7.2.1
    • 我不知道如何将 nuget 与 powershell 一起使用,我从不使用它
    • 我在 powershell 中运行以下命令?
    • 当我在 powershell 中运行它时出现错误“找不到与名称“版本”匹配的参数。” @S_G
    【解决方案3】:

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

    【讨论】:

      【解决方案4】:

      我在这里找到了一些很好的信息,哪些 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_ScriptDirDLL_7.2.4BouncyCastle.Crypto.dll"
          "$my_ScriptDirDLL_7.2.4Common.Logging.Core.dll"
          "$my_ScriptDirDLL_7.2.4Common.Logging.dll"
          "$my_ScriptDirDLL_7.2.4itext.commons.dll"
          "$my_ScriptDirDLL_7.2.4itext.forms.dll"
          "$my_ScriptDirDLL_7.2.4itext.io.dll"
          "$my_ScriptDirDLL_7.2.4itext.kernel.dll"
          "$my_ScriptDirDLL_7.2.4itext.layout.dll"
          "$my_ScriptDirDLL_7.2.4Microsoft.Bcl.AsyncInterfaces.dll"
          "$my_ScriptDirDLL_7.2.4Microsoft.Extensions.DependencyInjection.Abstractions.dll"
          "$my_ScriptDirDLL_7.2.4Microsoft.Extensions.DependencyInjection.dll"
          "$my_ScriptDirDLL_7.2.4Microsoft.Extensions.Logging.Abstractions.dll"
          "$my_ScriptDirDLL_7.2.4Microsoft.Extensions.Logging.dll"
          "$my_ScriptDirDLL_7.2.4Microsoft.Extensions.Options.dll"
          "$my_ScriptDirDLL_7.2.4Microsoft.Extensions.Primitives.dll"
          "$my_ScriptDirDLL_7.2.4System.Diagnostics.DiagnosticSource.dll"
          "$my_ScriptDirDLL_7.2.4System.Memory.dll"
          "$my_ScriptDirDLL_7.2.4System.Runtime.CompilerServices.Unsafe.dll"
          "$my_ScriptDirDLL_7.2.4System.Threading.Tasks.Extensions.dll"
          "$my_ScriptDirDLL_7.2.4System.ValueTuple.dll"
          "$my_ScriptDirDLL_7.2.4Newtonsoft.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 }
      }
      

      【讨论】:

        猜你喜欢
        • 2016-01-30
        • 2010-09-30
        • 2019-02-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-01-10
        • 1970-01-01
        • 2013-07-04
        相关资源
        最近更新 更多