Creating and merging PDFs on Linux


There are a number of ways that you can create PDFs on a Linux system. You can use an application like LibreOffice or OpenOffice, or you can take advantage of any of a number of commands that can generate PDFs from text files or from a group of other file formats. There are also a number of ways that you can merge a group of PDFs into a single PDF file.

Why PDFs?

PDF is often the preferred format for files that you need to share with others or archive. This is because PDF is an open file format, which makes sharing these files between diverse systems and devices possible.

Using LibreOffice or OpenOffice

Both LibreOffice and OpenOffice can export files as PDFs. You can open an existing document or create a new document and export it as a PDF. OpenOffice’s Export as PDF… and LibreOffice’s Export As => Export as PDF… will do what is required to convert your file.

Generating PDFs from text files

There are also a number of ways that you can create PDFs from text files. The commands below convert a text file to Postscript using vim and then turn the Postscript file into a PDF.

$ vim testfile.txt -c "hardcopy > testfile.ps | q"
$ ps2pdf testfile.ps

The resulting files might look something like these:

$ ls -l testfile*
-rw-rw-r-- 1 shs shs 7928 Feb 24 12:46 testfile.pdf
-rw-rw-r-- 1 shs shs 5864 Feb 24 12:45 testfile.ps
-rw-rw-r-- 1 shs shs   60 Feb 23 17:26 testfile.txt

Generating PDFs from html

Another option for generating PDFs is to convert html files. You can open an html file in either LibreOffice or OpenOffice and then export it as a PDF.

After opening an html file in OpenOffice, select File and, under that, Export… and select PDF with the Save as type: option to generate the PDF file.

After opening an html file in LibreOffice, save it in PDF format by using the Export As and Export as PDF… options.

Joining PDFs into a single file

One of the easiest ways to join a number of PDFs into a single file is to use the pdftk command. You can read more about using this command on an Ubuntu system at linuxhint.com.

A pdftk command to join a number of PDFs into a single file might look like this:

$ pdftk recipe-1.pdf recipe-2.pdf recipe-3.pdf recipe-4.pdf recipe-5.pdf cat output recipes.pdf

You can use a wildcard if your files follow a regular pattern like this:

$ pdftk recipe-*.pdf cat output recipes.pdf

pdfunite

Another command that works like pdftl that you can use is the pdfunite tool. As with pdftk, you can either list all of the files on your command line or using a wildcard if the file names follow a reliable pattern.

$ pdfunite recipe-1.pdf recipe-2.pdf recipe-3.pdf recipe-4.pdf recipe-5.pdf cat output recipes.pdf
$ pdfunite recipe-*.pdf recipes.pdf

Wrap-up

Using a word processor application on your Linux system or any of a number of commands, some of which you might have to install, you can easily work with, create or merge PDF files.

Join the Network World communities on Facebook and LinkedIn to comment on topics that are top of mind.

Copyright © 2021 IDG Communications, Inc.



Source link