首页 > 解决方案 > 使用 Powershell 生成 PDF

问题描述

我正在尝试使用 PowerShell 在 PDF 文件中从 SharePoint 检索到的数据(SharePoint 部分没问题)生成一个数组,但我真的不知道如何做到这一点。

我想要生成这样 的 PDF

编辑(1):

# Function to query list item from SharePoint
function Get-Items ($ListName, $BEGIN, $END, $IDUSER) {

    # Init variable as null
    $LibItems = $null
    $BEGIN = $BEGIN.ToString("yyyy-MM-dd")
    $END = $END.ToString("yyyy-MM-dd")

    $LibItems = Get-PnPListItem -List $ListName -Query "<View><Query><Where><And><Eq><FieldRef Name='id_client'/><Value Type='Text'>$IDUSER</Value></Eq><And><Geq><FieldRef Name='date_intervention'/><Value Type='DateTime'>$BEGIN</Value></Geq><Leq><FieldRef Name='date_intervention'/><Value Type='DateTime'>$END</Value></Leq></And></And></Where></Query></View>"

    # Return value
    return $LibItems
}

这就是我从 SharePoint 得到的(我没有故意查询所有字段)。

它给了我一个对象。

Id    Title                                              GUID                                                           
--    -----                                              ----                                                           
586                                                      cee9a0f7-206b-4240-ac42-fbb04e9c44b0                           
587                                                      cc402248-64cb-4e43-9164-1a42bc893cca                           
588                                                      eec5fc30-2408-42c1-aba2-c545600d9777                           
589                                                      6220ddb9-ad96-4c92-bc63-f57b93be18e2                           
652                                                      8433f5fb-70dc-4f96-904f-5376156d6e3d                           
653                                                      76ef46f4-ee6a-4e34-9244-011d0a6022aa                           
654                                                      d399f517-3fe8-43d0-abc9-6f022d66000c                           
656                                                      3f2e5db9-d122-4275-872d-2c5bbcb9f9db                           
657                                                      69b5a90f-bd8e-4735-beef-e13c7903cc47                           
702                                                      03010fc1-c386-4941-9446-ba67cd840260                           
703                                                      4759b602-07ed-4980-bee4-f4d3af36836a                           
704                                                      322aa276-b690-47c3-a401-60ade7b4f187                           
705                                                      96a0b295-6a55-40cd-9880-e580cefde5f1

System.Object[]                           

我尝试了 iTextSharp 但我没有设法让它正常工作......

标签: arrayspowershellpdf

解决方案


将您的输出复制到 texfile 并随后打印为 PDF:

Add-Type -AssemblyName System.Drawing

function ConvertTo-PDF {
    param(
        [Parameter(Mandatory, ValueFromPipeline, Position = 0)]
        $textDocumentPath
    )

    process {   
        $doc = New-Object System.Drawing.Printing.PrintDocument
        $doc.DocumentName = $textDocumentPath
        $doc.PrinterSettings = new-Object System.Drawing.Printing.PrinterSettings
        $doc.PrinterSettings.PrinterName = 'Microsoft Print to PDF'
        $doc.PrinterSettings.PrintToFile = $true
        $file = [io.fileinfo]$textDocumentPath
        $pdf  = [io.path]::Combine($file.DirectoryName, $file.BaseName) + '.pdf'
        $doc.PrinterSettings.PrintFileName = $pdf
        $doc.Print()
        $doc.Dispose()
    }
}


# Function to query list item from SharePoint
function Get-Items ($ListName, $BEGIN, $END, $IDUSER) {

    # Init variable as null
    $LibItems = $null
    $BEGIN = $BEGIN.ToString("yyyy-MM-dd")
    $END = $END.ToString("yyyy-MM-dd")

    $LibItems = Get-PnPListItem -List $ListName -Query "<View><Query><Where><And><Eq><FieldRef Name='id_client'/><Value Type='Text'>$IDUSER</Value></Eq><And><Geq><FieldRef Name='date_intervention'/><Value Type='DateTime'>$BEGIN</Value></Geq><Leq><FieldRef Name='date_intervention'/><Value Type='DateTime'>$END</Value></Leq></And></And></Where></Query></View>"

    # Return value
    return $LibItems
}


$libItems = Get-Items -ListName "<listName>" -BEGIN "<Begin>" -END "<End>" -IDUSER "<IdUser>"

$libItems | Out-File -FilePath 'D:\test\test.txt'

ConvertTo-PDF -textDocumentPath 'D:\test\test.txt'

推荐阅读