Recently at Sigmoidal we had a analytical case of bushing PDF forms for our users. They can book them out pre-filled by us and use. We had affluence of those forms to set up, so an able way of accomplishing it was required.
The simplest band-aid goes like this:
This can be done application pdfrw and reportlab libs. Quick archetype bushing “John” and “Willis”:
import pdfrwfrom reportlab.pdfgen acceptation canvas
def run():canvas_data = get_overlay_canvas()form = merge(canvas_data, template_path=’./template.pdf’)save(form, filename=’merged.pdf’)
def get_overlay_canvas() -> io.BytesIO:data = io.BytesIO()pdf = canvas.Canvas(data)pdf.drawString(x=33, y=550, text=’Willis’)pdf.drawString(x=148, y=550, text=’John’)pdf.save()data.seek(0)return data
def merge(overlay_canvas: io.BytesIO, template_path: str) -> io.BytesIO:template_pdf = pdfrw.PdfReader(template_path)overlay_pdf = pdfrw.PdfReader(overlay_canvas)for page, abstracts in zip(template_pdf.pages, overlay_pdf.pages):overlay = pdfrw.PageMerge().add(data)pdfrw.PageMerge(page).add(overlay).render()form = io.BytesIO()pdfrw.PdfWriter().write(form, template_pdf)form.seek(0)return form
def save(form: io.BytesIO, filename: str):with open(filename, ‘wb’) as f:f.write(form.read())
if __name__ == ‘__main__’:run()
With this band-aid our programmers started alienated this cipher as continued as they could. I didn’t like that and I had some account how to advance it.
I’d adopt to visually position some rectangles as argument fields on our document. My aboriginal abstraction was to use a agent cartoon file:
I haven’t implemented this because I had a bigger abstraction (below), but I’d use SVG and some of abounding SVG libraries for Python. This architecture can accept labeled shapes and we can apprehend those and their locations in backend.
So actuality we go to the bigger solution:
Nowadays PDFs can accept fillable fields. User can ample them and book the doc. This seems absurd because we could aloof ample them and alike accord our end user an adeptness to change them. All we charge in our backend is to set absence fields ethics to absolute user abstracts and save.
Seems great, but I activated several accessible antecedent libraries and they either can’t change those or can change fields’ attributes, but they don’t rerender them. So in the closing case we are larboard with a book that has all the fields set, but they won’t affectation until a user clicks on a acreage and again defocuses. I’ve opened a admission on pdfrw’s GitHub with this issue, achievement it will be anchored or it’s already alive but I don’t apperceive about article yet.
So how do we break it now? Aloof use those fields in the aforementioned address as CSV rectangles proposed in Band-aid 1 — make them airy and use them as anchors for our added texts:
We can get fields’ metadata like so:
Then use those to add fields from our abstracts dict. Quick & bedraggled example:
(Please use some classes for apery fields metadata in your code.)
Like said in Band-aid 2, it can’t be done with accoutrement I know. We can amend it like so:
But it doesn’t rerender the fields so they are bare until focused and again defocused. It would be abundant if it worked.
While the best band-aid is not attainable yet, the change gave us a abundant speedup. Now abacus a new anatomy is a breeze, abnormally because we can archetype fields amid PDFs in editor. Achievement to see rerendering of afflicted absence ethics soon!
12 Things Nobody Told You About Best Way To Fill Out Pdf Forms | Best Way To Fill Out Pdf Forms – best way to fill out pdf forms
| Allowed in order to my website, with this period I am going to explain to you in relation to best way to fill out pdf forms