Add text over existing PDF files with reportlab

I am interested in filling in the existing PDF formats programmatically. All I really need to do is output the information from user input, and then put the appropriate text on top of the existing PDF file in the appropriate locations. I can already do this with reportlab, loading the same sheet of paper into the printer twice, but it just spoils me wrong.

I am tempted to just personally redesign each existing PDF file and draw each line and character before adding user-entered text, but I wanted to check if there was an easy way to take an existing PDF file and set this as the background for some additional text. I would prefer to use python as this is the only language I feel comfortable with.

I also understand that I could just scan the document itself and use the resulting bitmap as a background, but I would prefer the accuracy of vector graphics.

ReportLab seems to have a commercial product with this functionality , and the specific function I'm looking for is in it (copyPages) - but it seems like an overpayment for a 4-digit product for a simple simple function for non-commercial use.

+6
python pdf pdf-generation reportlab
source share
3 answers

If the PDF forms are real AcroForms , you can use iText to fill them out. I don’t know if there is a different port than iText (java, original) and iTextSharp (C #), but it is easy to use and free if you do not mind opening your open source solution. You can see this sample code or (java snippet):

String formFile = "/path/to/myform.pdf" String newFile = "/path/to/output.pdf" PdfReader reader = new PdfReader(formFile); FileOutputStream outStream = new FileOutputStream(newFile); PdfStamper stamper = new PdfStamper(reader, outStream); AcroFields fields = stamper.getAcroFields(); // fill the form fields.setField("name", "Shane"); fields.setField("url", "http://stackoverflow.com"); // PDF infos HashMap<String, String> infoDoc = new HashMap<String, String>(); infoDoc.put("Title", "your title here"); infoDoc.put("Author", "JRE ;)"); stamper.setMoreInfo(infoDoc); // Flatten the PDF & cleanup stamper.setFormFlattening(true); stamper.close(); reader.close(); outStream.close(); 
+5
source share

If you just want to add some texts to pre-printed paper. You can scan it as jpg and then put this jpg as background. Please refer to page 15 of the reportlab manual, just call drawImage

+2
source share

It looks like you just need to place your existing PDF in the background of the Reportlab PDF you created. The free PDFRW library can do this easily. Take a look at the Tool Examples page for some specific examples of this technique.

0
source share

All Articles