admin管理员组文章数量:1297005
I am working on a project in spring-boot that signs visually pdfs.
The idea is that I am researching different ways to manipulate the pdfs using java and cannot find the most optimal one.
For example I have a template PDF:
Hi, my name is [Full Name], this is my signature [Signature]
I am wondering if there is any way to set variables/placeholders
or something like this in a pdf (which will serve as the global template), then at any moment to replace the variables/placeholders
with users specific data.
My implementation generates the pdf with personalized data from scratch , but I want the user to be able to upload the template pdf, so the flow would be like this:
- upload the template pdf (
multipartfile
request), - backend logic finds what to replace (user's name, date, signature)
- replace and return the personalized pdf
The only way that comes to mind to implement this, is to convert the uploaded pdf to xml or other format, find the fields that should to be replaced with user's data, replace it and convert it back to pdf, but I think this method will take a lot of resources.
I know that using iText or PDFBox libraries we can insert text/images on specific coordinates, but is not accurate and does not delete the text.
I am working on a project in spring-boot that signs visually pdfs.
The idea is that I am researching different ways to manipulate the pdfs using java and cannot find the most optimal one.
For example I have a template PDF:
Hi, my name is [Full Name], this is my signature [Signature]
I am wondering if there is any way to set variables/placeholders
or something like this in a pdf (which will serve as the global template), then at any moment to replace the variables/placeholders
with users specific data.
My implementation generates the pdf with personalized data from scratch , but I want the user to be able to upload the template pdf, so the flow would be like this:
- upload the template pdf (
multipartfile
request), - backend logic finds what to replace (user's name, date, signature)
- replace and return the personalized pdf
The only way that comes to mind to implement this, is to convert the uploaded pdf to xml or other format, find the fields that should to be replaced with user's data, replace it and convert it back to pdf, but I think this method will take a lot of resources.
I know that using iText or PDFBox libraries we can insert text/images on specific coordinates, but is not accurate and does not delete the text.
Share Improve this question asked Feb 12 at 3:14 c8ac8a 12 bronze badges 9- Would form fields (which your software would fill in and flatten for signing) be an option? – mkl Commented Feb 12 at 6:20
- 1 I would also say just use form fields. A textfield for the name and a signature field for the signature. Then you could really sign the PDF and not just add an image... – Lonzak Commented Feb 12 at 8:03
- @Lonzak how can I create a signature field in a pdf? The free sources I found on internet allow us only to create textfields, the ones that give the opportunity to create signature/image fields require payment. Also I found that we can create these text/image fields in Microsoft Word, but after 'Save as PDF' the fields are no longer fillable, they are transformed in just raw text. – c8a Commented Feb 12 at 10:45
- @mkl yes it would be, I already tested the app with textfields (for name and date), all I have to do is test how the image fields work but I cannot find any free tool to create signature/image fields. – c8a Commented Feb 12 at 11:02
- "all I have to do is test how the image fields work but I cannot find any free tool to create signature/image fields." - You have tagged your question itext and pdfbox. Both those libraries support creating signature fields (for digital signatures). You mention image fields. The PDF standard does not define image fields. Some PDF processors emulate image fields by using push buttons and playing around with the appearance of that push button using JavaScript. This is a mere emulations. But if you want, you can do that, too, and expect a push button where the image should go. – mkl Commented Feb 12 at 14:31
1 Answer
Reset to default -1So the big question here is "Do you really need the user to submit a PDF as a template for your feature set to work?" If the answer is No, I'd consider using a simpler format like Markdown. Something at the intersection of user and developer friendly. PDFs, while maybe are a bit user friendlier, they are definitely NOT developer friendly to parse. Doing something as simple as a text based template like this:
[salutation]
Hi ${name},
[/salutation]
[body]
This is a simple template that you can evaluate variables within this text and insert the output of this into the PDF Like so ${someVariable}. You can make this as complicated as you wish with sections denoted with [sectionName] and endings terminated like [/sectionName] so you could insert these sections into parts of the PDF when the output of the template doesn't go in one nice spot.
[/body]
[signature]
Your Friendly Developer,
${devName}
[/signature]
Using something like the above is pretty easy to perform the substitution then render that into a PDF with PDFBox. The overall pipeline for this is evaluate the template and a set of variables to produce one or more String segments. Then build the PDF using the PDFBox API, and insert your Strings and static content into your PDFDocument and write that out. I find using HTML as a basis to be very flexible as a layout language for PDFs and easy to render to a PDF. I've used OpenPDF for this and it works great. The only downside is CSS support is very limited so nothing fancy...or modern.
But parsing a PDF, while it is doable with enough knowledge of the PDF format, it'll be a headache because how PDFs can be authored is complex! And copying their entire object model into a new PDF document with your substitutions will require a lot of testing. It's possible, but you may get more mileage out of keeping the template format simple.
If your users have to have specially formatted elements you might consider allowing them to provide those things to you: logos, statements, etc. And just add those to your template you build. You could offer manually customizing say HTML template for a user.
本文标签: spring bootData replacement in a PDF Template (JavaSpringBoot)Stack Overflow
版权声明:本文标题:spring boot - Data replacement in a PDF Template (Java | SpringBoot) - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741623455a2388947.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论