Dawara
Dawara

Reputation: 11

Generate PDF with Powershell

I'm trying to generate an array with data retrieved from SharePoint (The SharePoint part is ok) in a PDF file with PowerShell but i have literally no idea how to do this.

I want something like this PDF to generate

Edit (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
}

That's what i get from SharePoint (I didn't query all the fields on purpose).

It gives me an Object.

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[]                           

I tried iTextSharp but i didn't manage to get it work properly...

Upvotes: 1

Views: 9808

Answers (1)

f6a4
f6a4

Reputation: 1782

Copy your Output to a texfile and print as PDF afterwards:

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'

Upvotes: 3

Related Questions