Expand my Community achievements bar.

SOLVED

Converting HTML to PDF and saving the pdf in backend

Avatar

Level 3

Hi,

i have a requirement in which i will be hitting a url(http://example.com/home/index?buID=123) where buID is the URL parameter. With respect to the url parameter buID some html are generated using jquery.

I want to convert the  generated html(or entire webpage) after hitting the above url to pdf.

Can anyone let me know is these the optimal solution for the above?

1) getting entire html in a variable using jquery and passing it as a parameter to servlet where logic for converting html to pdf is written(ajax call) as shown

var text = $('#mycontent').html().toString();

var doc = new DOMParser().parseFromString(text, 'text/html');

var result = new XMLSerializer().serializeToString(doc);

console.log(result);

          $.ajax({

type: 'GET',  

url: '/bin/example/pdfGenerationServlet',

          data: {'text': result},

                     success: function(msg){

                      console.log(msg);

                     },

                    error:function(text){});

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi,

If you can change your url and add selector then you can register servlet based on selector/extenstion and can be able to convert page from servlet without using jquery. e.g. http://example.com/home/index.genpdf?buID=123

Convert AEM page to PDF

If you want to stick with URL then you need to make Ajax call to call servlet. I think there is no need to pass html to servlet, may be you can pass url of the page based on the API you are using.

Thanks
Arun



Arun Patidar

View solution in original post

2 Replies

Avatar

Correct answer by
Community Advisor

Hi,

If you can change your url and add selector then you can register servlet based on selector/extenstion and can be able to convert page from servlet without using jquery. e.g. http://example.com/home/index.genpdf?buID=123

Convert AEM page to PDF

If you want to stick with URL then you need to make Ajax call to call servlet. I think there is no need to pass html to servlet, may be you can pass url of the page based on the API you are using.

Thanks
Arun



Arun Patidar

Avatar

Level 7

Hi

Instead of passing the entire HTML as a parameter to servlet you can try to get the HTML of the webpage from backend itself using the path of the page.

You can use the following block of code to retrieve the HTML from page path:

public String getHTML(String filePath, SlingHttpServletRequest request,

   RequestResponseFactory requestResponseFactory, SlingRequestProcessor requestProcessor)

   throws ServletException, IOException {

  HttpServletRequest req = requestResponseFactory.createRequest("GET", filePath);

  WCMMode.DISABLED.toRequest(req);

  ByteArrayOutputStream os = new ByteArrayOutputStream();

  HttpServletResponse resp = requestResponseFactory.createResponse(os);

  requestProcessor.processRequest(req, resp, request.getResourceResolver());

  String output = "";

  output = os.toString(CharEncoding.UTF_8);

  return output;

}

Hope this helps!

Regards,

TechAspect Solutions