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 xps2pdf.sh contents to your bashrc file. If you don't know what that means, then run the following:

curl -fsSL https://gist.githubusercontent.com/balupton/7f15f6627d90426f12b24a12a4ac5975/raw/xps2pdf.sh | 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.

#!/bin/sh
# 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"
fi
}
# XPS to PDF
function xps2pdf {
local cwd=$(pwd)
local bin=gxps
if command_missing $bin; then
bin=$HOME/bin/ghostpdl-9.20/bin/gxps
if command_missing $bin; then
echo "downloading and compiling gxps dependency to $bin"
mkdir -p $HOME/bin
cd $HOME/bin
down https://github.com/ArtifexSoftware/ghostpdl-downloads/releases/download/gs920/ghostpdl-9.20.tar.gz
untar ghostpdl-9.20.tar.gz
rm ghostpdl-9.20.tar.gz
cd ./ghostpdl-9.20
./configure
make
cd $cwd
fi
fi
echo "converting $# files"
for xps in "$@"
do
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"
done
}

afrozalm commented Aug 9, 2017 edited

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