Consider the following procedure:

1) Open a PDF file using the (Ubuntu) document viewer.

2) Goto File

3) Goto Print

4) Choose 'Print as file' and check the mark "pdf"

Yes, I am printing a PDF as PDF file. I have to do this for a large collection of files. I would like to do this running a script at the terminal.

Maybe you think that this procedure does not make any sense at all, but it actually was the only safe way I found to deal with the issue considered here. This StackOverflow question shows a problem with PDF files the EOF marker is not found. When I do the procedure described above, I am able to solve this problem [This is not the solution presented in StackOverflow site, which does not work here.]

I would like to this automatically with all files that present this issue.

I know that I should build one script (.sh) to this, but I do not know how to proceed.

An example of the PDF file (1) with problems is here

The corrected PDF file (2) is here

The only difference between them is that I opened the first using the "Ubuntu Document Viewer", I went to "File", "Print", "Print to File", "Output=pdf".

I want to make this automatically.


I do NOT want to merge the files.

  • 1
    Do you want to print them (on paper, using a printer) or "save them in another directory from terminal"? – terdon Dec 12 '18 at 12:29
  • Save in another directory as pdf. – DanielTheRocketMan Dec 12 '18 at 12:36
  • However, I do not want to copy them. – DanielTheRocketMan Dec 12 '18 at 12:38
  • 1
    So you want to copy them? Are you just looking for a script that will run cp orig.pdf copy.pdf for each file? You've linked to an SO question with a lot of text and a lot of python, why is that relevant? Please edit and explain what you need. – terdon Dec 12 '18 at 12:38
  • 1
    "The pdfminer error mentioned above": what is this error? How can we reproduce it so we can test our answers? If it is explained in the linked Stack Overflow question, then please reproduce the relevant information here. – terdon Dec 12 '18 at 15:11

Make sure you have cups-pdf installed and added as printer. Note pdf printer name. (Optional) Edit /etc/cups/cups-pdf.conf: uncomment and change the line where is says #Out /var/spool/cups-pdf/${USER} to the desired output directory.

Then restart CUPS: sudo systemctl restart cups

Now in order to print do:

for i in "$SOURCEDIR"
    lpr -P <pdf printer name> "$i"

Now copy the files from the default output directory to the desired location, if you didn't edit cups-pdf.conf

Install poppler-utils and run pdfunite input1.pdf input2.pdf input3.pdf output.pdf.

| improve this answer | |
  • There are about hundred pdfs – DanielTheRocketMan Dec 12 '18 at 12:45
  • And you want to make them all 100 into one pdf? – spacelander Dec 12 '18 at 12:47
  • No I want to print each pdf file as a file one by one. – DanielTheRocketMan Dec 12 '18 at 15:00
  • This way to handle the problem is very in-effective and may lead to new problems with the modified files. A better method would be to use Ghostscript directly.... – Kurt Pfeifle Dec 26 '18 at 0:56

You can achieve what you want most easily with the help of Ghostscript. I successfully tested this with Ghostscript version 9.26:

gs                  \
  -o out.pdf        \
  -sDEVICE=pdfwrite \

I must admit, I do not really comprehend your problem with the EOF marker in the unmodified example file here, because I can clearly see this EOF marker being present in it.

However, I see an additional problem which shows in your 'corrected' example file here: this one now uses a page size of A4 (595 x 842 pts) with large white margins, whereas your unmodified file has small margins and a page size of 362 x 558 pts (which is less than half of A4!)

My method has the advantage to preserve the original page sizes.

To apply it to all PDF files in the current directory, including all sub-directories, use the following command:

find . -name "*.pdf" -type f | while read line; do
    gs -o mod-${line} -sDEVICE=pdfwrite "${line}"
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.