15

I have a PDF that I want to show to someone without giving them the ability to copy it. One possible way would be to convert pages to images, then these back into a PDF. How can I use convert or pdftk or otherwise script? Not GUI.

6
  • 2
    Often they will still be able to simply run OCR and copy the text anyway. Just saying.
    – Fiksdal
    Nov 3, 2016 at 18:36
  • In fact my ASUS laptop came with Foxit PhantomPDF viewer/printer. One time it upgraded itself over the net, and there was a limited time free trial feature activated that did OCR. Worked quite nicely changing a scan-only PDF into something I could search and cut/paste from (but I'm still too cheap to buy a new copy)
    – infixed
    Nov 3, 2016 at 20:40
  • Adding security options to a pdf document to make it uncopyable/unprintable/unviewable under certain circumstances is built into the file format, you just have to enable it. I've only done this with Acrobat though, I can't recommend a package to do it via script. I'm interested to see if anyone else knows of one.
    – Cody
    Nov 3, 2016 at 21:15
  • This is a security by obscurity problem. They will always have the ability to copy your text regardless of what you do to try and stop them. At best you can make it very difficult, but never impossible
    – Darren H
    Nov 4, 2016 at 4:43
  • "at best you can make it very difficult": that's an interesting programming problem, maybe the software can add noise that would confuse OCR programs but it's pretty much invisible to humans. Still not a solution though, they can just pay a data entry person to get it out as text.
    – Sundar R
    Nov 4, 2016 at 14:25

5 Answers 5

27

Imagemagick rasterizes everything. You could simply do:

convert -density 200 input.pdf output.pdf

Of course the recipient might decide to run an OCR program on your file. The density parameter sets the resolution expressed as PPI.

13

PDF to Image

The poppler-utils packages includes the pdftoppm utility, capable of converting PDF files to either ppm, png or jpeg format:

pdftoppm -png file.pdf prefix

This will make files name prefix-X.extension where X is the page number (each file outputted will be one page of the PDF converted) and where extension is whatever output type you choose.

By default the resolution is 150dpi. You can increase the resolution (for higher quality output) by using this command:

pdftoppm -rx 300 -ry 300 -extension file.pdf prefix

And to print only one page, do:

pdftoppm -f N -singlefile -extension file.pdf prefix

where N is the page number, starting with 1.

This method is a lot faster and less clunky than using the imagemagick package as mentioned in other posts. Although you do have to use it to convert back.

Image to PDF

This requires the installation of the imagemagick package. To do this, do:

sudo apt-get install imagemagick

The imagemagick package has a utility called convert which will do just as it is named; convert. To make use of it in the way that you want, run it like so:

convert file.extension file.pdf

This will make a PDF of only that single page, though. To combine all outputs of the previous command for converting to images, use this command:

convert *.extension file.pdf

This will grab all files in the directory that you are in with the extension extension and convert them into a PDF file named file.pdf.

Note

I chose to format my answer in the way of two separate commands as to give the OP flexibility and understanding of the task that they are trying to complete, instead of one command linking the two. Of course, if that solution is better for them, then I encourage them to upvote/mark as answered that particular answer.

2
  • Suggesting a way to split the task in two parts is good for flexibility. But it may be noted that the first part can be done with convert as well. :) Nov 3, 2016 at 15:04
  • @AndreaLazzarotto I know that it can. But, I chose pdftoppm because it is faster and lighter than convert. I just went the route of most efficient and only used convert because there was no reverse command for pdftoppm.
    – TheOdd
    Nov 3, 2016 at 15:07
4

I always use pdftoppm to convert pdf to png images. simply execute the following command

pdftoppm -f [startpage] -l [end page] -r [resolution (use 300 or 600) -png <pdf file location> [Prefix for your image files]

and then execute

convert prefix-[number].png file.pdf

but there are programs that convert these images back into text. making your pdf uncopyable is very hard. You could add an encryption protection to prevent copying and printing which will make is (slightly) more difficult to copy and print your pdf file

1

I routinely have to perform the operation. This is the way I usually do it.

First assign name of file to variable fileToFlatten:

fileToFlatten=NameOfFile.pdf

Since you need to be in the same folder as your file, type-ahead will work fine by pressing Tab. Especially useful if you have spaces for instance in your file name.

Then run one-liner script:

pdftoppm "$fileToFlatten" -r 150 tmpImage && img2pdf tmpImage*.ppm -o tmpBitmapPDF.pdf && rm tmpImage*.ppm && gs -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/ebook -sOutputFile="${fileToFlatten%????}"_btmp.pdf tmpBitmapPDF.pdf && rm tmpBitmapPDF.pdf

Several temporary files are created and then deleted in the folder during the execution of the script.

You can adjust the dpi of the file with the -r 150 (could be 300 for more definition for instance).

The ghostscript command (gs) is used to add a layer of compression to the file. In my experience it reduces the file size significantly for a very limited cost in visible compression.

-2

Yes, you can first convert your PDF pages to PNG/JPG images, then convert the PNG/JPG to PDF.

However, the person you send your file to can use an OCR service to get the text from your file.

3
  • Rather than link to content elsewhere which may disappear perhaps you could expand your answer a bit more. Mar 25, 2020 at 4:06
  • Links should be listed only for reference. Please list out the steps to take to convert such files. Also, please note that the original question explicitly states that there is no GUI (graphical user interface), so a link to a website that does the conversions for you is not going to work.
    – Gordster
    Mar 25, 2020 at 4:52
  • @StephenMichaelKellat This is not a link-only answer as the link is actually the answer. It points to an online PDF converter. May 27, 2021 at 13:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .