Expand my Community achievements bar.

Enhance your AEM Assets & Boost Your Development: [AEM Gems | June 19, 2024] Improving the Developer Experience with New APIs and Events
SOLVED

Create Pdf in AEM DAM Programatically

Avatar

Level 2

I want to create a pdf file based on inputs from user and store the file in DAM. I tried creating a pdf file using this code :

 

InputStream stream = new ByteArrayInputStream(attachmentData.getBytes(StandardCharsets.UTF_8));
final ValueFactory valueFactory = session.getValueFactory();
final Binary binary = valueFactory.createBinary(stream);
assetManager.createOrReplaceAsset(path, binary, "application/pdf", true);

 

but the pdf file is not created properly and do not show data.

 

Can anyone help me on this?

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi @anshul__ ,

The code you provided seems to be creating an asset in the DAM with a PDF binary. However, it's important to note that simply creating an asset with a binary stream may not result in a valid PDF file.

To create a PDF file with proper content, you typically need to use a library or framework that supports generating PDFs, such as Apache PDFBox or iText. These libraries provide APIs to create and manipulate PDF documents programmatically.

Here's an example using Apache PDFBox to generate a PDF with content and store it in the DAM:

 

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;

import javax.jcr.Node;
import javax.jcr.Session;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

// Obtain a ResourceResolver instance using the ResourceResolverFactory

ResourceResolverFactory resolverFactory = ... // Obtain the ResourceResolverFactory instance
ResourceResolver resolver = resolverFactory.getServiceResourceResolver(null);

// Create a new PDF document
PDDocument document = new PDDocument();

// Create a new page
PDPage page = new PDPage();
document.addPage(page);

// Create a content stream for the page
PDPageContentStream contentStream = new PDPageContentStream(document, page);

// Write some text to the content stream
contentStream.beginText();
contentStream.setFont(PDType1Font.HELVETICA_BOLD, 12);
contentStream.newLineAtOffset(100, 700);
contentStream.showText("Hello, World!");
contentStream.endText();
contentStream.close();

// Save the PDF document to a ByteArrayOutputStream
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
document.save(outputStream);
document.close();

// Convert the ByteArrayOutputStream to a Binary
Binary binary = valueFactory.createBinary(new ByteArrayInputStream(outputStream.toByteArray()));

// Specify the DAM path where you want to store the PDF
String damPath = "/content/dam/mypath/mypdf.pdf";

// Adapt the ResourceResolver to a Session
Session session = resolver.adaptTo(Session.class);

// Create or replace the asset in the DAM
AssetManager assetManager = resolver.adaptTo(AssetManager.class);
Asset asset = assetManager.createOrReplaceAsset(damPath, binary, "application/pdf", true);

// Commit the changes to the session
session.save();
session.logout();

 

This code demonstrates how to create a PDF document using Apache PDFBox, add content to it, convert it to a Binary, and then store it as an asset in the DAM. You'll need to adjust the DAM path and handle exceptions appropriately in your implementation.

Remember to include the necessary dependencies for Apache PDFBox in your project's build configuration.

 

https://pdfbox.apache.org/1.8/cookbook/documentcreation.html

 

View solution in original post

2 Replies

Avatar

Correct answer by
Community Advisor

Hi @anshul__ ,

The code you provided seems to be creating an asset in the DAM with a PDF binary. However, it's important to note that simply creating an asset with a binary stream may not result in a valid PDF file.

To create a PDF file with proper content, you typically need to use a library or framework that supports generating PDFs, such as Apache PDFBox or iText. These libraries provide APIs to create and manipulate PDF documents programmatically.

Here's an example using Apache PDFBox to generate a PDF with content and store it in the DAM:

 

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;

import javax.jcr.Node;
import javax.jcr.Session;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

// Obtain a ResourceResolver instance using the ResourceResolverFactory

ResourceResolverFactory resolverFactory = ... // Obtain the ResourceResolverFactory instance
ResourceResolver resolver = resolverFactory.getServiceResourceResolver(null);

// Create a new PDF document
PDDocument document = new PDDocument();

// Create a new page
PDPage page = new PDPage();
document.addPage(page);

// Create a content stream for the page
PDPageContentStream contentStream = new PDPageContentStream(document, page);

// Write some text to the content stream
contentStream.beginText();
contentStream.setFont(PDType1Font.HELVETICA_BOLD, 12);
contentStream.newLineAtOffset(100, 700);
contentStream.showText("Hello, World!");
contentStream.endText();
contentStream.close();

// Save the PDF document to a ByteArrayOutputStream
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
document.save(outputStream);
document.close();

// Convert the ByteArrayOutputStream to a Binary
Binary binary = valueFactory.createBinary(new ByteArrayInputStream(outputStream.toByteArray()));

// Specify the DAM path where you want to store the PDF
String damPath = "/content/dam/mypath/mypdf.pdf";

// Adapt the ResourceResolver to a Session
Session session = resolver.adaptTo(Session.class);

// Create or replace the asset in the DAM
AssetManager assetManager = resolver.adaptTo(AssetManager.class);
Asset asset = assetManager.createOrReplaceAsset(damPath, binary, "application/pdf", true);

// Commit the changes to the session
session.save();
session.logout();

 

This code demonstrates how to create a PDF document using Apache PDFBox, add content to it, convert it to a Binary, and then store it as an asset in the DAM. You'll need to adjust the DAM path and handle exceptions appropriately in your implementation.

Remember to include the necessary dependencies for Apache PDFBox in your project's build configuration.

 

https://pdfbox.apache.org/1.8/cookbook/documentcreation.html

 

Avatar

Level 1

I have done using iText as PDF Box have more complexity but good Controls for drafting the PDF.

import com.day.cq.dam.api.Asset;
import com.day.cq.dam.api.AssetManager;
import com.itextpdf.text.Document;
import com.itextpdf.text.Font;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;

import javax.jcr.*;
import java.io.*;

ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

Document doc = new Document();
Font boldFont = new Font();
boldFont.setStyle(Font.BOLD);

PdfWriter writer = PdfWriter.getInstance(doc, outputStream);

doc.open();

doc.add(new Paragraph("  Hello World!   "));
doc.add(new Paragraph("   ");
doc.add(new Paragraph("   End of the paragraph  "));

doc.close();
outputStream.close();

session = resourceResolver.adaptTo(Session.class);

ValueFactory valueFactory = session.getValueFactory();

Binary binary = valueFactory.createBinary(new ByteArrayInputStream(outputStream.toByteArray()));
String damPath = "/content/dam/newPDF.pdf";

AssetManager assetManager = resourceResolver.adaptTo(AssetManager.class);

Asset asset = assetManager.createOrUpdateAsset(damPath, binary, "application/pdf", true);

session.save();