Skip to content
This repository was archived by the owner on Dec 15, 2023. It is now read-only.
This repository was archived by the owner on Dec 15, 2023. It is now read-only.

Problem adding PDF with PowerShell #1

Description

Not sure what I am doing wrong, but when I try to insert a PDF using the OneNote API the PDF is unreadable and doesn’t render.

$tokenfile = get-content <…snip…>.json | ConvertFrom-Json
$access_token = $tokenfile.$access_token
foreach ($file in gci "/Users/steven/Documents/Receipts/2020 Receipts\*.pdf"){
    $date = [datetime]::ParseExact($file.BaseName.Substring(0,17), 'yyyy-MM-dd HHmmss', $null)
    $pdffile = [System.IO.File]::ReadAllBytes($file.FullName)
    $pageContent = @"
--MyPartBoundary
Content-Disposition:form-data; name="Presentation"
Content-Type:text/html

<!DOCTYPE html>
<html>
<head>
<title>$($file.BaseName -replace ".pdf","")</title>
<meta name="created" content="$($date.ToUniversalTime().ToString('u').replace(' ','T'))" />
</head>
<body>
<p><img data-render-src="name:FileBlock" /></p>
<p><object data-attachment="$($file.name)" data="name:FileBlock" type="application/pdf" /></p>
</body>
</html>
--MyPartBoundary
Content-Disposition:form-data; name="FileBlock"
Content-Type:application/pdf

$pdfFile
--MyPartBoundary--
"@

Invoke-RestMethod -Uri 'https://graph.microsoft.com/v1.0/me/onenote/sections/<…snip...>/pages' `
    -Method Post `
    -ContentType 'multipart/form-data; boundary=MyPartBoundary' `
    -Headers @{"Authorization" = "Bearer $($tokenfile.access_token)"} `
    -Body $pageContent
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions