0

I am working on a Spring-MVC project in which we have users registering for us. Now, whenever an user registers, we would like to fill out the information like name, address, etc of the user while keeping some parts of it constant, like our name, address, etc. Once it's filled out, convert it into PDF and attach it into an email. I have the form available as a doc, pdf, html. I already have the code to attach the file in an email.

What's the best strategy to go forward in this problem? Thank you.

We are Borg
  • 5,117
  • 17
  • 102
  • 225
  • Looking at your reps, you know how to ask a question. Post some code man. :) – Chirag Parmar Nov 04 '16 at 10:21
  • @ChiragParmar : No code yet man, I am just trying to find the best approach to solve this problem. Like if I should use HTML form and fill it out or somethign else. – We are Borg Nov 04 '16 at 10:30

2 Answers2

2

The best solution is to have the form available as a PDF form (AcroForm technology). You can then use a PDF library to fill out that form with the data retrieved from the HTML form.

See for instance: How to fill out a pdf file programatically?

PdfReader reader = new PdfReader(src);
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
AcroFields form = stamper.getAcroFields();
form.setField(key, value);
stamper.setFormFlattening(true);
stamper.close();
reader.close();

If you remove the line stamper.setFormFlattening(true); the form will still be interactive. By adding that line, you remove all form fields (and the interactivity).

You could even ask people to fill out the PDF form as is done here: Edit pdf embedded in the browser and save the pdf directly to server

I think that Spring ships with (a mighty old version of) iText. You might want to use a recent version. Consult the official web site to learn more about the differences between iText 5 and iText 7.

Community
  • 1
  • 1
Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
1

You can make use of JasperReport utility of java. In that what you need to do is create one standard Jasper template (.jasper extension file) where you can configure as many field you want and fill that report run time using java code while keep things fixed like your name,address that you dont need to feel with every user request when you got your user request object.

Jasper has the facility to fill this template in backend and it allows you to generate pdf run time which you can attach in email response where you want.

Firstly, the input JSON:

{ 
    "userName": "Evil Raat",
    "details": {
        "email": "not_really@test.com"
    }
}

Then create a JSON DataSource in iReport Designer and point it at your file (leaving all the other details as their defaults)

Next you can use the following jrxml template to render the above JSON into a report:

<?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="sample" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="a894078a-929b-4aae-a1d0-46485f0f8835">
    <property name="ireport.zoom" value="1.0"/>
    <property name="ireport.x" value="0"/>
    <property name="ireport.y" value="0"/>
    <queryString language="json">
        <![CDATA[]]>
    </queryString>
    <field name="userName" class="java.lang.String">
        <fieldDescription><![CDATA[userName]]></fieldDescription>
    </field>
    <field name="userEmail" class="java.lang.String">
        <fieldDescription><![CDATA[details.email]]></fieldDescription>
    </field>
    <title>
        <band height="200" splitType="Stretch">
            <textField>
                <reportElement uuid="3b74775b-4555-43c3-bdf2-1677145c8660" x="0" y="31" width="555" height="20"/>
                <textElement textAlignment="Right">
                    <font fontName="Helvetica" size="12" isBold="true"/>
                </textElement>
                <textFieldExpression><![CDATA[$F{userName}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement uuid="aa6cc7c8-2ca1-4f0f-92e2-c466083daba0" x="0" y="54" width="555" height="20"/>
                <textElement textAlignment="Right">
                    <font fontName="Helvetica" size="12" isBold="true"/>
                </textElement>
                <textFieldExpression><![CDATA[$F{userEmail}]]></textFieldExpression>
            </textField>
        </band>
    </title>
</jasperReport>

Than you can create code like ,

Resource resource = new ClassPathXmlApplicationContext().getResource("classpath:reports/project.jrxml");
JsonDataSource ds = new JsonDataSource(new File("c:\myjson.json"));
jasperDesign = JRXmlLoader.load(resource.getInputStream());
JasperReport jasperReport  = JasperCompileManager.compileReport(jasperDesign);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport,  parameters,  ds);
JasperExportManager.exportReportToPdfFile(jasperPrint, destination+fileName+".pdf");

In above code i have use .json file as source of data to fill report. You can runtime generate json and provide it while filling report.

Chirag Parmar
  • 833
  • 11
  • 26