pdf generation - Generate flattened PDF with Python -
when print pdf of source pdfs, file size drops , removes text boxes presents in form. in short, flattens file. behavior want achieve.
the following code create pdf using pdf source (the 1 want flatten), writes text boxes form well.
can pdf without text boxes, flatten it? adobe when print pdf pdf.
my other code looks minus things:
import os import stringio pypdf import pdffilewriter, pdffilereader reportlab.pdfgen import canvas reportlab.lib.pagesizes import letter directory = os.path.join(os.getcwd(), "source") # dir interested in fif = [f f in os.listdir(directory) if f[-3:] == 'pdf'] # pdfs in fif: packet = stringio.stringio() can = canvas.canvas(packet, pagesize=letter) can.rotate(-90) can.save() packet.seek(0) new_pdf = pdffilereader(packet) fname = os.path.join('source', i) existing_pdf = pdffilereader(file(fname, "rb")) output = pdffilewriter() nump = existing_pdf.getnumpages() page = existing_pdf.getpage(0) l in range(nump): output.addpage(existing_pdf.getpage(l)) page.mergepage(new_pdf.getpage(0)) outputstream = file("out-"+i, "wb") output.write(outputstream) outputstream.close() print fname + " written as",
summing up: have pdf, add text box it, covering info , adding new info, , print pdf pdf. text box becomes not editable or moveable longer. wanted automate process tried still allowed text box editable.
if installing os package option, use pdftk
python wrapper pypdftk
this:
import pypdftk pypdftk.fill_form('filled.pdf', out_file='flattened.pdf', flatten=true)
you need install pdftk
package, on ubuntu done this:
sudo apt-get install pdftk
the pypdftk
library can downloaded pypi:
pip install pypdftk
Comments
Post a Comment