Skip to content

Instantly share code, notes, and snippets.

@balupton balupton/README.md
Last active Oct 21, 2019

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

Convert XPS to PDF on Linux and Mac

  1. Download this file

  2. chmod +x ./xps2pdf.bash

  3. To convert a particular xps file: ./xps2pdf.bash thefile.xps

  4. To convert all xps files in the current working directory: ./xps2pdf.bash *.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.

Extracted from https://github.com/balupton/dotfiles/blob/master/.scripts/commands/xps2pdf

#!/usr/bin/env bash
# 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
cwd=$(pwd)
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
tar -zxvf 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
file=$(echo "$xps" | sed 's/...$//')
pdf=${file}pdf
echo "converting $xps to $pdf"
$bin -sDEVICE=pdfwrite -sOutputFile="$pdf" -dNOPAUSE "$xps"
if command_exists GetFileInfo && command_exists SetFile; then
echo "setting creation time"
local ctime=$(GetFileInfo -m "$xps")
SetFile -d "$ctime" "$pdf"
fi
done
@afrozalm

This comment has been minimized.

Copy link

commented Aug 9, 2017

Getting the following errors:

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

@balupton

This comment has been minimized.

Copy link
Owner Author

commented Jun 6, 2018

@afrozalm okay try the latest script

@FrankLingwen

This comment has been minimized.

Copy link

commented Oct 21, 2019

Thanks for your good article, if you want convert from XPS files to PDF files, VeryUtils XPS to PDF Converter is a good choice, you can download and try it from this web page: https://veryutils.com/xps-to-pdf-converter-command-line

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.