Make a PDF file read-only/flatten in python

851 Views Asked by At

I have a PDF file(generated using HTML) that contains text and bar-code image. I want to convert/make the PDF such that the text or image is not selectable.

I have tried the following packages but it's not working

  1. fillpdf
  2. pypdftk
1

There are 1 best solutions below

1
On

Two alternatives:

  1. Convert pages to images in a new PDF
  2. Encrypt the PDF not permitting unwanted features.
import fitz  # PyMuPDF
doc = fitz.open("input.pdf")
out = fitz.open()  # output PDF

for page in doc:
    w, h = page.rect.br  # page width / height taken from bottom right point coords
    outpage = out.new_page(width=w, height=h)  # out page has same dimensions
    pix = page.get_pixmap(dpi=150)  # set desired resolution
    outpage.insert_image(page.rect, pixmap=pix)
out.save("output.pdf", garbage=3, deflate=True)

Second alternative: encrypt the PDF with owner and user passwords, letting the owner have a minimum of permissions:

import fitz
perm = int(  # permissions bit flags
    fitz.PDF_PERM_ACCESSIBILITY # always use this
    | fitz.PDF_PERM_PRINT # permit printing
)
owner_pass = "owner"  # owner password
user_pass = "user"  # user password
encrypt_meth = fitz.PDF_ENCRYPT_AES_256  # strongest algorithm
doc.save("output.pdf",
    encryption=encrypt_meth,  # set the encryption method
    owner_pw=owner_pass,  # set the owner password
    user_pw=user_pass,  # set the user password
    permissions=perm,  # set permissions
)

However, to be really sure about that all PDF viewers will well-behave, I recommend using the first method.