Hi,
I am trying to convert html to pdf on AWS lambda using Java. I am getting out of memory error while executing the AWS Lambda.
The code tries to read html from s3 bucket and convert to pdf and upload to s3 bucket.
Though if try to run locally as a standalone program it runs fine. However it errors while executing on the AWS lambda.
The error i get is
CrossChargeHTML2PDFGenerator.main() Entering
CrossChargeHTML2PDFGenerator.generateHtmlToPdf() Entering Method
CrossChargeHTML2PDFGenerator.generateHtmlToPdf() Input Stream Done
Exception in thread “main”
Exception: java.lang.OutOfMemoryError thrown from the UncaughtExceptionHandler in thread “main”
Exception in thread “SpringApplicationShutdownHook”
Exception: java.lang.OutOfMemoryError thrown from the UncaughtExceptionHandler in thread “SpringApplicationShutdownHook”
Here is the code snippet for the same.
/**
*
*/
package com.ocd.crosscharge.function;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.sql.Timestamp;
import java.util.function.Function;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.s3.model.PutObjectRequest;
import com.aspose.pdf.Document;
import com.aspose.pdf.HtmlLoadOptions;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
/**
- @author ASARKAR
*/
@Component
public class CrossChargeHTML2PDFGenerator implements Function<String, String> {
//private static final String HTML_INPUT = "htmlforopenpdf.html";
//private static final String PDF_OUTPUT = "src/main/resources/html2pdf.pdf";
private static final String BUCKET = "comm-xcharge-d-documents";
ObjectMapper objectMapper = new ObjectMapper();
static final Logger logger = LoggerFactory.getLogger(CrossChargeHTML2PDFGenerator.class);
@Override
public String apply(String input) {
String responseJson = "Executed Successfully";
logger.info("Input Request is" + input);
System.out.println("CrossChargeHTML2PDFGenerator.main() Entering");
try {
generateHtmlToPdf();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("CrossChargeHTML2PDFGenerator.main() Exiting");
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
return responseJson;
}
public void generateHtmlToPdf() throws Exception {
System.out.println("CrossChargeHTML2PDFGenerator.generateHtmlToPdf() Entering Method");
//String fileContent = s3client.getObjectAsString(BUCKET, "invtemplates/htmlforopenpdf.html");
AmazonS3 s3client = AmazonS3ClientBuilder.standard().build();
InputStream is = s3client.getObject(BUCKET, "invtemplates/htmlforopenpdf.html").getObjectContent();
System.out.println("CrossChargeHTML2PDFGenerator.generateHtmlToPdf() Input Stream Done");
HtmlLoadOptions htmloptions = new HtmlLoadOptions();
// Load stream into Document object
Document pdfDocument = new Document(is,htmloptions);
System.out.println("CrossChargeHTML2PDFGenerator.generateHtmlToPdf() pdf document created");
// Convert HTML file to PDF
ByteArrayOutputStream baos = new ByteArrayOutputStream();
pdfDocument.save(baos);
System.out.println("CrossChargeHTML2PDFGenerator.generateHtmlToPdf() Output Stream Done");
InputStream targetStream = new ByteArrayInputStream(baos.toByteArray());
Timestamp timestamp = new Timestamp(System.currentTimeMillis());
String fileName = "invoice-"+timestamp+".pdf";
s3client.putObject(new PutObjectRequest(BUCKET, fileName, targetStream, null));
System.out.println("CrossChargeHTML2PDFGenerator.generateHtmlToPdf() Exiting Method");
}