Expand my Community achievements bar.

SOLVED

PDF Creation in AEM

Avatar

Level 2

Hi everyone,
we need to create a PDF in AEM.
I don' t think there is an out of the box way / library to do it, but I may be wrong.
I have seen some old answers but the links to the solutions are not available anymore.
We don' t use AEM Forms on our instances and I don' know how hard it is to add it.
Anyway let me explain some details about the functionality creation we have to implement.
Basically the user compiles an html form and submits. An Ajax request is performed. We already have a service that does some business logic when called.
If submit is successful, in one div of the page just appears a text that confirms successful elaboration.
Now the idea is to show also a button to print a PDF based on a given template in which we should put some of the input entered by the user.
Which is the best solution ? Should we use a back end creation with a library like JasperReports / iReport ?
Or should we implement a client side solution with some Javascript library ?
Consider that at the moment we don' t have a web page that we can convert to PDF and that PDF file should have a header with company logo in each page. Maybe PDF file won' t be more than 2 pages long but one page is not enough.
We are also thinking about loading an html template on DAM, replacing some placeholders with user submitted data and converting it to pdf and of course serve it as a response.
I hope the requirements are clear.
Thanks in advance.

1 Accepted Solution

Avatar

Correct answer by
Level 7

Hi @AlleOTC,

It's a complex question to select proper technology. It depends on your use cases. Below you can find options that you can discover:

1. Backend PDF Generation

  • Recommended Library:

    • iText (Free for AGPL or licensed versions for commercial use).
    • Apache PDFBox (Open source but more basic than iText).
  • How to implement:

    1. Create a PDF Template: Store a template in AEM DAM (e.g., as an HTML or XML template).
    2. Fetch and Populate Data:
      • Retrieve the template from DAM.
      • Replace placeholders with the user’s submitted data.
    3. Generate PDF:
      • Use iText or Apache PDFBox to render the template into a PDF.
    4. Serve the PDF:
      • Use a servlet to serve the PDF file as a response.
  • Pros:

    • Full control over the PDF structure.
    • Reusable and scalable solution for server-side rendering.
    • Easily integrates with AEM’s back-end.
  • Cons:

    • Additional effort for template creation and maintenance.
    • Higher resource usage on the server.

2. Client-Side PDF Generation

  • Recommended Library:

    • jsPDF: Simple and effective for basic PDFs.
    • html2pdf.js: Converts HTML into a PDF (good for designing templates with CSS).
  • How to implement:

    1. Create a JavaScript function that uses jsPDF or html2pdf.js.
    2. When the user clicks the button, generate a PDF in the browser with their submitted data.
  • Pros:

    • Reduces server load.
    • Faster for users since no server-side processing is needed.
  • Cons:

    • Limited control over complex templates.
    • Dependence on the client’s browser capabilities.

3. HTML-to-PDF Conversion Using DAM Template

  • Steps:

    1. Upload an HTML template to AEM DAM.
    2. Use a server-side library like iText or a headless browser tool (e.g., Puppeteer) to convert the template into a PDF.
    3. Replace placeholders with user data dynamically before generating the PDF.
  • Pros:

    • Templates are easy to manage and update.
    • Combines the flexibility of HTML with server-side control.
  • Cons:

    • Requires more setup compared to other approaches.

Which Solution Is Best?

  • Backend generation (iText or similar) is ideal for your scenario because:

    • The PDF must include a logo and structured data, which is easier to handle server-side.
    • You can predefine and maintain a consistent template stored in the DAM.
    • You can ensure security and prevent browser compatibility issues.
  • If you prefer client-side generation, html2pdf.js is a good option but may have limitations in handling logos and multi-page PDFs.

Best regards,

Kostiantyn Diachenko.

View solution in original post

4 Replies

Avatar

Correct answer by
Level 7

Hi @AlleOTC,

It's a complex question to select proper technology. It depends on your use cases. Below you can find options that you can discover:

1. Backend PDF Generation

  • Recommended Library:

    • iText (Free for AGPL or licensed versions for commercial use).
    • Apache PDFBox (Open source but more basic than iText).
  • How to implement:

    1. Create a PDF Template: Store a template in AEM DAM (e.g., as an HTML or XML template).
    2. Fetch and Populate Data:
      • Retrieve the template from DAM.
      • Replace placeholders with the user’s submitted data.
    3. Generate PDF:
      • Use iText or Apache PDFBox to render the template into a PDF.
    4. Serve the PDF:
      • Use a servlet to serve the PDF file as a response.
  • Pros:

    • Full control over the PDF structure.
    • Reusable and scalable solution for server-side rendering.
    • Easily integrates with AEM’s back-end.
  • Cons:

    • Additional effort for template creation and maintenance.
    • Higher resource usage on the server.

2. Client-Side PDF Generation

  • Recommended Library:

    • jsPDF: Simple and effective for basic PDFs.
    • html2pdf.js: Converts HTML into a PDF (good for designing templates with CSS).
  • How to implement:

    1. Create a JavaScript function that uses jsPDF or html2pdf.js.
    2. When the user clicks the button, generate a PDF in the browser with their submitted data.
  • Pros:

    • Reduces server load.
    • Faster for users since no server-side processing is needed.
  • Cons:

    • Limited control over complex templates.
    • Dependence on the client’s browser capabilities.

3. HTML-to-PDF Conversion Using DAM Template

  • Steps:

    1. Upload an HTML template to AEM DAM.
    2. Use a server-side library like iText or a headless browser tool (e.g., Puppeteer) to convert the template into a PDF.
    3. Replace placeholders with user data dynamically before generating the PDF.
  • Pros:

    • Templates are easy to manage and update.
    • Combines the flexibility of HTML with server-side control.
  • Cons:

    • Requires more setup compared to other approaches.

Which Solution Is Best?

  • Backend generation (iText or similar) is ideal for your scenario because:

    • The PDF must include a logo and structured data, which is easier to handle server-side.
    • You can predefine and maintain a consistent template stored in the DAM.
    • You can ensure security and prevent browser compatibility issues.
  • If you prefer client-side generation, html2pdf.js is a good option but may have limitations in handling logos and multi-page PDFs.

Best regards,

Kostiantyn Diachenko.

Avatar

Level 2

Hi Konstantin,

thanks a lot for your reply, I will evaluate the solutions.

Bye.

Avatar

Level 2

Hi Konstantin,

I have to say that I don' t really understand the difference between solution 1 and 3.
I mean both make use of an html template loaded on DAM, and can use a library like iText.

Avatar

Level 7

Yes, they are almost the same. Using iText should be good in your case.