1. Introduction

Whether we’re preparing documents for printing or integrating them into specialized document management workflows, we may often be tasked with converting PDF files to PostScript.

In this tutorial, we’ll discuss different methods to convert PDF to PostScript. First, we’ll explore using pdf2ps, which is a Ghostscript interpreter. Lastly, we’ll utilize the pdftops utility from Poppler utils.

2. Using pdf2ps

Ghostscript is a versatile interpreter for the PostScript language and PDF files. One of its utilities, pdf2ps, facilitates the conversion of PDF files to PostScript format. Since pdf2ps is usually available by default in Linux, there’s often no need to install it.

2.1. Converting a Single File Using pdf2ps

To use pdf2ps, we can open a new terminal and use the pdf2ps utility to convert a PDF file:

$ pdf2ps file1.pdf output.ps

In the above command, we use the pdf2ps utility to convert the file1.pdf file, which is a PDF document, into a PostScript file, output.ps.

2.2. Converting Multiple Files Using pdf2ps

To automate the conversion of several .pdf files to the .ps format, we can create a Bash script to search for the required files using a loop.

Let’s view the script using the cat command:

$ cat pdf2psConversion.sh
#!/bin/bash
echo "searching for PDF files"
find . -type f -iname '*.pdf' -print0 | while IFS= read -r -d '' file
do
    echo "Found $file"
    pdf2ps "${file}" "${file%.*}.ps"
    echo "conversion successful"
done

Now, we make the file executable using the chmod command, and run it:

$ chmod +x pdf2psConversion.sh
$ ./pdf2psConversion.sh
searching for PDF files
Found ./file1.pdf
conversion successful
Found ./file2-pdfjam.pdf
conversion successful
Found ./file2.pdf
conversion successful

The above script searches for the required PDF files in the current directory and subdirectories. Let’s break down each part of the script:

  • find . -type f -iname ‘*.pdf’ -print0 searches the current directory and all its subdirectories for files with names ending in .pdf
  • while IFS= read -r -d ‘ ‘ file reads each NULL-delimited filename that is output by the find command and stores it in the variable file
  • pdf2ps “${file}” “${file%.*}.ps” converts each PDF file to PostScript format using the pdf2ps utility

In particular, ${file%.*} is a parameter expansion that removes the .pdf extension from the filename. Moreover, it adds .ps to create the output PostScript file. Additionally, we can check the converted PostScript file content from the File Explorer or via the terminal.

In summary, this script efficiently converts all PDF files found in the current directory and its subdirectories to .ps format using the pdf2ps command within a loop structure.

3. Using pdftops

Another approach for PostScript conversion is using pdftops.

pdftops or xPDF is another collection of tools for working with PDF files. Moreover, it’s a part of poppler-utils, a collection of command-line utilities built on the Poppler library API.

3.1. Converting a Single File

To convert a PDF file to the .ps format, we use pdftops, passing filenames as arguments:

$ pdftops file2.pdf output.ps

When we execute this command, pdftops reads the contents of the input PDF file. Next, it converts it to PostScript format and saves the resulting PostScript file as output.ps.

3.2. Converting Multiple Files in a Directory

Additionally, we can convert all the available PDF files in a directory using pdftops by modifying the script discussed previously. Let’s look at an example:

$ cat pdftopsConversion.sh
#!/bin/bash
find . -type f -iname '*.pdf' -print0 |
while IFS= read -r -d '' file
do
     pdftops "${file}" "${file%.*}.ps"
done

Next, we make this script executable, and run it:

$ chmod +x pdftopsConversion.sh
$ ./pdftopsConversion.sh

The above script converts all .pdf files to .ps files. In this script, we use the find command to find all files with a case-sensitive name ending in .pdf. Moreover, the -print0 option handles filenames with spaces or special characters.

Next, we use a while loop to continuously read the filenames coming from the find command. The loop reads each NULL-delimited filename output by find. Additionally, the use of IFS ensures leading and trailing whitespace characters are preserved. Moreover, -r prevents backslash interpretation, and -d ‘ ‘ sets the delimiter to NULL.

Subsequently, the script executes the pdftops command inside the loop. For each PDF file, this script uses pdftops to convert the PDF file to PostScript format by using the parameter expansion.

Lastly, the script names the output files by removing the extension from the input filename and appending .ps to it.

4. Conclusion

In this article, we learned ways to convert PDF to PostScript. Initially, we discussed the Ghostscript approach. Subsequently, we explored the pdf2ps approach to obtain similar results.

Crucially, pdf2ps may take longer to convert large PDF files, as compared to pdftops, since pdf2ps converts regular fonts into bitmap fonts.

Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.