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.
EDIT:
I do NOT want to merge the files.
92 Answers
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 \ in.pdfI 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}"
done 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:
SOURCEDIR="/path/to/source/pdfs"
for i in "$SOURCEDIR"
do lpr -P <pdf printer name> "$i"
doneNow 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.