Skip to content

Instantly share code, notes, and snippets.

@balupton balupton/
Last active Oct 21, 2019

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

#!/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"
# XPS to PDF
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
tar -zxvf ghostpdl-9.20.tar.gz
rm ghostpdl-9.20.tar.gz
cd ./ghostpdl-9.20
cd $cwd
echo "converting $# files"
for xps in "$@"
file=$(echo "$xps" | sed 's/...$//')
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"

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


This comment has been minimized.

Copy link
Owner Author

commented Jun 6, 2018

@afrozalm okay try the latest script


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:

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.