-
-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathxps2pdf
executable file
·90 lines (70 loc) · 2.29 KB
/
xps2pdf
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/usr/bin/env bash
# Sample XPS file available at:
# https://github.com/microsoft/Windows-classic-samples/blob/67a8cddc25880ebc64018e833f0bf51589fd4521/Samples/Win7Samples/xps/XpsLoadModifySave/sample1.xps
# Thanks to [Sriram Thaiyar](https://github.com/sri) for the [original ruby version](https://github.com/sri/xps2pdf/) which figured out [GhostPDL](https://ghostscript.com/download/gpdldnld.html) can be used for the conversion.
# This command is referenced in this gist:
# https://gist.github.com/balupton/7f15f6627d90426f12b24a12a4ac5975
# @todo merge into [convert-helper]
function xps2pdf_() (
source "$DOROTHY/sources/bash.bash"
# =====================================
# Arguments
function help {
cat <<-EOF >/dev/stderr
ABOUT:
Convert from XPS (XML Paper Specification) to PDF, using GXPS from GhostPDL from GhostScript.
USAGE:
xps2pdf [--] ...<path>
EXAMPLES:
xps2pdf -- *.xps # all .xps files in the current directory
xps2pdf -- one.xps two.xps # specific files
QUIRKS:
If supported, the PDF files will be created with the same creation time as the original XPS files.
EOF
if [[ $# -ne 0 ]]; then
echo-error "$@"
fi
return 22 # EINVAL 22 Invalid argument
}
# process
local item option_paths=()
while [[ $# -ne 0 ]]; do
item="$1"
shift
case "$item" in
'--help' | '-h') help ;;
'--')
option_paths+=("$@")
shift "$#"
break
;;
'--'*) help "An unrecognised flag was provided: $item" ;;
*) option_paths+=("$item") ;;
esac
done
# check
if [[ ${#option_paths[@]} -eq 0 ]]; then
help 'No <path>s provided.'
fi
# =====================================
# Dependency
setup-util-ghostscript
# =====================================
# Action
local xps pdf ctime
echo-style --bold="Converting " --code="${#paths[@]}" --bold=" files"
for xps in "${option_paths[@]}"; do
pdf="${xps%.xps}.pdf"
echo-style --bold="Converting " --code="$xps" --bold=" to " --code="$pdf"
gxps -sDEVICE=pdfwrite -sOutputFile="$pdf" -dNOPAUSE "$xps"
if __command_exists -- GetFileInfo SetFile; then
ctime="$(GetFileInfo -m "$xps")"
SetFile -d "$ctime" "$pdf"
fi
echo-style --success="Converted " --code="$xps" --success=" to " --code="$pdf"
done
)
# fire if invoked standalone
if [[ $0 == "${BASH_SOURCE[0]}" ]]; then
xps2pdf_ "$@"
fi