Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Convert XPS to PDF on Linux and Mac using bash

Convert XPS to PDF on Linux and Mac

Add the contents to your bashrc file. If you don't know what that means, then run the following:

curl -fsSL | bash

Use like so:

# Convert a particular xps file
xps2pdf thefile.xps

# Convert all xps files in the current working directory
xps2pdf *.xps

It will create the pdf files with the same creation time as the original xps files.

Thanks to Sriram Thaiyar for the original ruby version which figures out GhostPDL can be used for the conversion, which is the crux of the operation.

# Extras
function command_exists {
type "$1" &> /dev/null
# fish not bash: type --quiet "$1"
function command_missing {
! command_exists "$1"
# Download a file
alias download='down'
function down {
# do not use the continue flags as they will prefer the local file over the remote file if the local exists
if command_exists aria2c; then
aria2c --allow-overwrite=true --auto-file-renaming=false "$1"
elif command_exists wget; then
wget -N "$1"
elif command_exists curl; then
curl -OL "$1"
elif command_exists http; then
http -d "$1"
# XPS to PDF
function xps2pdf {
local cwd=$(pwd)
local bin=gxps
if command_missing $bin; then
if command_missing $bin; then
echo "downloading and compiling gxps dependency to $bin"
mkdir -p $HOME/bin
cd $HOME/bin
untar ghostpdl-9.20.tar.gz
rm ghostpdl-9.20.tar.gz
cd ./ghostpdl-9.20
cd $cwd
echo "converting $# files"
for xps in "$@"
local file=$(echo "$xps" | sed 's/...$//')
local pdf=${file}pdf
echo "converting $xps to $pdf"
$bin -sDEVICE=pdfwrite -sOutputFile="$pdf" -dNOPAUSE "$xps"
local ctime=$(GetFileInfo -m "$xps")
SetFile -d "$ctime" "$pdf"

afrozalm commented Aug 9, 2017

Getting the following errors:

xps2pdf:28: command not found: GetFileInfo
xps2pdf:29: command not found: SetFile

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment