-
Notifications
You must be signed in to change notification settings - Fork 0
/
render_to_pdf.ps1
executable file
·54 lines (40 loc) · 1.63 KB
/
render_to_pdf.ps1
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
param (
[string]$code = "",
[string]$combine = ""
)
$ErrorActionPreference = "Stop"
if ($code -eq "") {
Write-Host "Usage: "
Write-Host " Rendering one course: $($MyInvocation.MyCommand.Name) <code>"
Write-Host " Rendering all courses: $($MyInvocation.MyCommand.Name) all"
exit 1
}
if ($code -eq "all") {
$folders = Get-ChildItem "out\G*" -Directory
foreach ($folder in $folders) {
$folderName = $folder.Name
Invoke-Expression "./$($MyInvocation.MyCommand.Name) $folderName $combine"
}
exit 0
}
Write-Host "Processing ${code}..."
try {
& ".\render.ps1" ${code}
} catch {
Write-Host "An error occured in $_"
throw
}
$outFolder = "out\$code"
$tempFolder = "out/${code}_pdfbook"
Remove-Item -Path $tempFolder -Recurse -Force -ErrorAction SilentlyContinue
New-Item -ItemType Directory -Path $tempFolder | Out-Null
Copy-Item -Path "$outFolder/*" -Destination $tempFolder -Recurse -Force
Move-Item -Path "$tempFolder/index.html" -Destination "$tempFolder/00_index.html" -Force
docker run --rm -v "$(pwd)/process_html.sh:/app/run.sh" -v "$(pwd)/out:/out" --entrypoint /bin/sh surnet/alpine-wkhtmltopdf:3.19.0-0.12.6-small /app/run.sh $code
$pdfFolder = "out/${code}_pdf"
if ($combine -ne "no-combine") {
docker run --rm -v "$(pwd)/out:/out" --entrypoint /bin/sh aminnairi/pdfunite -c "pdfunite /${pdfFolder}/*.pdf /${pdfFolder}/${code}.pdf"
Get-ChildItem -Path $pdfFolder -Filter "*.pdf" | Where-Object { $_.Name -ne "$code.pdf" } | Remove-Item -Force
}
Remove-Item -Path $tempFolder -Recurse -Force
Write-Host "Process completed! Output at $(pwd)\out\${code}_pdf" -ForegroundColor Green