EDIT: NOV 15, 2022 – it seems that the result of my ‘solution’ below using the convert command may come with compromised quality. Try it out as a backup but the first solution with the gs command gave me excellent results. I have left both for you in case and put them in relevant order.
Today someone send me a PDF with a filename like this: ‘filename.docx.pdf’.
Already suspicious, I know, so when I double clicked to open it in Document Viewer software I was not surprised to get this message:
“Unable to open document: ‘path/to/broken/file/filename.docx.pdf’. PDF document is damaged”
I knew that the Convert software in the terminal is absolutely amazing for converting, say, a jpg to a pdf, or a png to a jpg, so, I thought to myself “I wonder what would happen if I converted this broken PDF to a PDF?”
As fate would have it, the result was that it fixed the broken PDF and after performing this I could double click and open it like normal. Cool.
How to do it – New (Best Quality) Solution with Ghost Script (gs) command
Simply read my [other blog about compressing pdfs] and put in your broken / fixed files and paths into that tutorial and boom. Fixed PDF at high resolution! Hope that helps
How to do it – Old (Less Quality) Solution with Imagemagik convert comand
(see notes above – this didn’t provide a high resolution solution so use this method as a backup perhaps?)
- Open a terminal
- Test to make sure you have the imagemagik software installed. If you are using Ubuntu (standard) it should be there. Test by typing
convertin your terminal and hitting the enter key. You should see something like this followed by a bunch of instructions and features you can use with it:
Version: ImageMagick 6.9.10-23 Q16 x86_64 20190101 https://imagemagick.org
If you discovered it’s not installed you’ll have to install the imagemagik package. Search how to do this online.
- In the terminal, navigate to where your broken PDF is with the
cdcommand. TIP: If it’s buried deep, a quick trick is to right click and copy your file to clipboard and then control + shift + v paste into your terminal. This will give you the path to your file which might be easier than typing it all out or navigating for a long time? Make sure to remove the filename at the end of the path though since that’s a file, not a path…
convertinto the terminal followed by the
pathname/to/broken/pdf/filename.docx.pdf(broken file path with borken file filename) and then leave one space and type
pathname/to/broken/pdf/filename_fixed.pdf(broken file path with FIXED filename at end) adjusting all the paths and file names to your need. So, the command before pressing enter should look something like this:
convert pathname/to/broken/pdf/filename.docx.pdf pathname/to/broken/pdf/filename_fixed.pdf
- Hit ‘enter’ – it should do something silently and simply clear your terminal screen. Doesn’t really look like anything happened, but if no error came back, it probably worked just fine.
- Go back to where your broken file was
You should now see your ‘filename_fixed.pdf’ sitting there. Try opening it like normal to see if it works.
Hope it did and hope this helps