-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path01-basic.ps1
More file actions
60 lines (52 loc) · 2.49 KB
/
Copy path01-basic.ps1
File metadata and controls
60 lines (52 loc) · 2.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# sign/01-basic.ps1 — Sign a rendered PDF with a self-signed certificate
#
# Prerequisites:
# - pdfnative-cli installed globally: npm install -g pdfnative-cli
# - openssl available on your PATH (ships with Git for Windows / OpenSSL for Windows)
#
# Usage:
# pwsh -File samples\sign\01-basic.ps1
#
# Output: samples\output\sign\01-basic-signed.pdf
$ErrorActionPreference = 'Stop'
$ScriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
$RootDir = Split-Path -Parent (Split-Path -Parent $ScriptDir)
$OutputDir = Join-Path $RootDir 'samples\output'
$SignOut = Join-Path $OutputDir 'sign'
$KeysDir = Join-Path $SignOut 'keys'
New-Item -ItemType Directory -Force -Path $SignOut | Out-Null
New-Item -ItemType Directory -Force -Path $KeysDir | Out-Null
$UnsignedPdf = Join-Path $OutputDir 'document\02-report.pdf'
$SignedPdf = Join-Path $SignOut '01-basic-signed.pdf'
$KeyFile = Join-Path $KeysDir 'signing.key'
$CertFile = Join-Path $KeysDir 'signing.crt'
# ── Step 1: render the source document (if not already rendered) ────────────
if (-not (Test-Path $UnsignedPdf)) {
Write-Host '→ Rendering source document…'
New-Item -ItemType Directory -Force -Path (Split-Path $UnsignedPdf) | Out-Null
& pdfnative render `
--input (Join-Path $RootDir 'samples\render\document\02-report.json') `
--output $UnsignedPdf
Write-Host " ✓ Rendered: $UnsignedPdf"
}
# ── Step 2: generate self-signed certificate (for demo only) ────────────────
if (-not (Test-Path $KeyFile) -or -not (Test-Path $CertFile)) {
Write-Host '→ Generating self-signed certificate (demo)…'
& openssl req -x509 -newkey rsa:2048 `
-keyout $KeyFile -out $CertFile `
-days 365 -nodes `
-subj '/CN=pdfnative Demo/O=pdfnative/C=US' 2>$null
Write-Host " ✓ Key: $KeyFile"
Write-Host " ✓ Cert: $CertFile"
}
# ── Step 3: sign the PDF ────────────────────────────────────────────────────
Write-Host '→ Signing PDF…'
& pdfnative sign `
--input $UnsignedPdf `
--output $SignedPdf `
--key $KeyFile `
--cert $CertFile
Write-Host " ✓ Signed: $SignedPdf"
Write-Host ''
Write-Host 'Done. Verify the signature with:'
Write-Host " pdfnative inspect --input `"$SignedPdf`" --format json | Select-String -Pattern 'sign'"