从powershell生成pdf

omqzjyyz  于 2023-02-16  发布在  Shell
关注(0)|答案(4)|浏览(153)

我正在尝试用powershell生成一个PDF文件,但是我不知道该怎么做。我已经试过使用itext7,但是我不知道如何让它工作。
当我尝试在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.

能帮我个忙吗?
先谢了

mkh04yzy

mkh04yzy1#

PowerShell依赖项的组合可能会有问题,因为它们需要***及时成为已知的工作组,而7.1.14被吹捧为一个轻量级解决方案***,因此请参见稍后的TL;DR编辑或其他评论如下,***[稍后编辑以获取更近期的7.2更详细列表。#依赖项请参见@paul-oneill的答案](https://stackoverflow.com/a/75280860/10802527)***并以可能不同于普通用户的管理员身份运行。因此,请小心执行这些步骤,因为有些人可能会降级您的当前设置。
最重要的是,使用项目目录并查看提示符是否位于该文件夹中,以确保您没有在默认PowerShell目录中运行。我使用的快捷方式中,目标目录为“blank/empty”,因此默认为当前工作文件夹。
第1次检查:-

project folder>[Net.ServicePointManager]::SecurityProtocol

应该返回Tls 12或Tls 13,我们需要它为1.2,因此请注意您的设置是否为Tls 13,并运行此行。

[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

现在你应该能够安装dlls如下

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

仔细检查文件夹是否具有正确的结构x1c 0d1x
请注意,脚本的顺序和位置对于正确加载至关重要

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混合版本工作时没有太多冲突,目标是生成一组在Net 40环境中工作的独立文件,但是随着时间的推移,您应该确保使用的是更新的组合。然而,在7. 1. 15中所有内容都发生了变化因为Net4.5和4.6.1现在需要的依赖项列表明显更多,尽管如此,packages/itext 7/7.2.1本身仍然可以与packages/Portable.BouncyCastle/1.8.9 +一起工作,并且公共日志记录仍然是3.4.1

ykejflvf

ykejflvf2#

以下是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()
hts6caw3

hts6caw33#

我在这里找到了一些很好的信息,哪些DLL需要通过add-type加载... https://renenyffenegger.ch/notes/design/graphic/pdf/tools/iText/index
通过反复试验,我发现在itext 7版本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 }
}
mlnl4t2r

mlnl4t2r4#

只是我的2美分,但上述代码不适用于itext 77. 2. 1(修改后的正确路径)。
真希望我上周看到这篇文章--浪费了几天的时间,在7.2.1上拔头发,没有表现出来。

相关问题