![]() Then draw rectangles at the locations you want to add text and then use your new image as a config file for filling out the PDF. One of them was to take a PDF and save the pages as a series of images. I have read about several other ways to “fill” the fields in these kinds of PDFs. But you might want to keep an eye on these as they might hint at a problem should you have any issues. tokens.py:226 stream /Length attribute (171) appears to be too small (size 470) - adjusting (line=192, col=1)Īs I mentioned, this doesn’t actually prevent the merged PDF from being created. Note: When I ran this code, I did receive some errors on stdout. At the end of the code, we create an instance of PdfWriter that we use to write the newly merged PDF out. ![]() Then we loop over the pages of both PDFs and merge them together using PageMerge. ![]() Here we open up both the form and the overlay PDFs using pdfrw’s PdfReader classes. Pdfrw.PageMerge(form_page).add(overlay).render() Merge the specified fillable form PDF with theįor form_page, overlay_page in zip(form.pages, olay.pages): Let’s write that function next:ĭef merge_pdfs(form_pdf, overlay_pdf, output): The next piece of the puzzle is actually merging the overlay we created above with the form we created in the previous section. We already knew where on the page the form elements were, so we can make a good educated guess of where to draw the strings to. Since we created the original form, figuring out the offset for the overlay is actually pretty easy. This is very helpful in figuring out the exact coordinates you need to draw your strings to. Fortunately on Linux and Mac, there are decent PDF Previewer applications that you can use to just keep the PDF open and they will automatically refresh with each change. We just use the drawString canvas method. Then we create a function called create_overlay that creates a simple PDF using ReportLab’s Canvas class. Here we import the pdfrw package and we also import the canvas sub-module from ReportLab. Let’s check that out:Ĭreate the data that will be overlayed on topĬ = canvas.Canvas('simple_form_overlay.pdf')Ĭ.drawString(115, 550, '123 Greenway Road') The first function will create our overlay. Now that we have those installed, let’s create a file called fill_by_overlay.py. Let’s go ahead and take a look at how this approach might work using the pdfrw package. You could theoretically use PyPDF2 for the merging process too. The author then used pdfrw to merge the two PDFs together. The first solution proposed was to take an unfilled form in a PDF and create a separate PDF using ReportLab that has the data we want to us to “fill” this form. Jan Chęć wrote an article on Medium that contained several different approaches to this problem of filling in forms in PDFs. Now we are ready to learn one of the ways that we can fill in this form! Merging Overlays When you run this example, the interactive PDF form looks like this: Here is the code:įrom import magenta, pink, blue, greenĬ.drawCentredString(300, 700, 'Employment Form')įorm.textfield(name='fname', tooltip='First Name',įorm.textfield(name='lname', tooltip='Last Name',įorm.textfield(name='address', tooltip='Address',įorm.textfield(name='city', tooltip='City',įorm.textfield(name='state', tooltip='State',įorm.textfield(name='zip_code', tooltip='Zip Code', ReportLab has built-in support for creating interactive forms, so let’s use ReportLab to create a simple form. We need a simple form to use for our first example. ![]() Then that might be a viable option because you then have complete control over the PDF creation and the inputs that need to go into it. Frankly I think this is probably the worst idea, except when your company is in charge of creating the PDFs itself. The most time consuming method I have heard about is to just recreate the form in ReportLab by hand and then fill it in. There are many different approaches for filling in these forms programmatically. There are lots of government forms that use fillable forms. One of the most famous examples of fillable forms in the United States are documents from the Internal Revenue Service. Fillable forms have been a part of Adobe’s PDF format for years.
0 Comments
Leave a Reply. |