Learn through the super-clean Baeldung Pro experience:
>> Membership and Baeldung Pro.
No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with.
Last updated: March 18, 2024
Portable Document Format (PDF) documents are widely used to share and exchange information. This is due to the fact that they’re versatile, portable, and can be viewed on various devices without compromising the formatting or layout.
In this tutorial, we’ll explore how to create a blank PDF document from the command line. First, we’ll discuss how to use the ghostscript command to achieve this. Next, we’ll use mutool, another command that will help us complete this task.
We’ll primarily focus on the Debian and Ubuntu Linux distributions.
ghostscript is a versatile command-line tool used for working with PostScript and PDF files. Therefore, we can use it to create a blank PDF document.
First, we need to install ghostscript on our system:
$ sudo apt install ghostscript
In the example above, we utilize apt, a command line tool that’s used to manage packages in Linux.
So, this installation enables us to invoke ghostscript with the help of the gs command. It follows a general syntax:
$ gs -o output_file_name.pdf -sDEVICE=pdfwrite -gpage_width x page_height -c "showpage"
The options above provide the gs command further instructions to help personalize the output.
Let’s review these options:
Next, let’s navigate to the Documents/ directory. This is where we’ll save our blank PDF document:
$ cd Documents/
Once we’re in this directory, let’s go ahead and create a blank PDF document:
$ gs -o blank.pdf -sDEVICE=pdfwrite -g6120x7920 -c "showpage"
Here, we create a new PDF file named blank.pdf that contains a single blank page. Also, the -g6120x7920 option sets the page size to 8.5 inches by 11 inches.
In addition to creating a single blank page, we can also create a PDF document with multiple blank pages. To do this, we simply need to add more “showpage” parameters to the -c option:
$ gs -o blank.pdf -sDEVICE=pdfwrite -g6120x7920 -c "showpage" -c "showpage" -c "showpage"
The above command creates a new PDF document named blank.pdf containing three blank pages.
mutool is a command-line tool for working with PDF files. In particular, we can use it to create, manipulate, and extract information from PDF files. In this case, we’ll use it to create a new blank PDF document.
Before we proceed, we need to install this tool:
$ sudo apt install mupdf-tools
This command helps to successfully install mutool from the mupdf-tools package.
Next, let’s take a look at the general syntax we’ll use to invoke mutool from the command line:
$ mutool create [options] output.pdf [input.pdf]
It contains these parameters:
Finally, let’s proceed with creating the document by using the syntax we discussed:
$ mutool create -o blank.pdf /dev/null
Using the instruction above, we’ve been able to create a blank PDF document named blank.pdf. To understand, the -o option specifies the name of the output file as blank.pdf. Moreover, we use /dev/null as the input file, which is a special file in Linux that discards all data written to it.
In this article, we discussed two approaches important for creating a blank PDF document in Linux.
We learned how to utilize ghostscript as well as the mutool command line tool to achieve this. We also explored some of the options to use with both commands to customize the output.