Using the PDF rewriter with custom xml output | Community
Skip to main content
Dterner
Level 2
October 16, 2015
Solved

Using the PDF rewriter with custom xml output

  • October 16, 2015
  • 7 replies
  • 1944 views

I was successful in generating a pdf file from our content using the PDF rewriter and page2fo.xsl . However we need to add some custom content to the generated XML. So we created a custom XML handler for that page component.

 Just to be clear we created:

apps/myproject/components/mycomponent/mycomponent.xml.jsp

The new handler works great, but the PDF rewriter is not picking that up. Anyone have any experience with this?

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by smacdonald2008

See if this helps you:

http://cqblueprints.com/tipsandtricks/pdfrendering.html

7 replies

smacdonald2008
Level 10
October 16, 2015

One thing you can look into is looking at the code located in the bundle:

http://blogs.adobe.com/sunil/2013/04/29/creating-pdf-output-in-adobe-cq/

Adobe Employee
October 16, 2015

@Dterner , Can you share a sample package for me to look at?

Dterner
DternerAuthor
Level 2
October 16, 2015

Hey all thanks for the help and @kalynar for the offer. The blueprints site does offer a clue and I now believe this approach is a dead end. 

relevant bit: "The only parameter sent in the xsl request is the parameter "resource," which holds a string of the current resource's path."

My assumption was that it gets the xml for the transform from the xml rendition but instead the rewriter directly accesses the jcr nodes of the given resource.

Dterner
DternerAuthor
Level 2
October 16, 2015

Thanks Scott, that is the documentation we looked at for the default implementation. I'm not sure what you mean, the documentation just goes through making sure the service is installed and how to write some custom xsl. We were able to do that. The issue is that the pdf rewriter is not following the standard sling resolutions and we are looking for a way of achieving that.

Adobe Employee
October 16, 2015

Please find an example using wkhtmltopdf exe.

                                                                                                                                                                                            
import org.apache.commons.io.IOUtils;
 import org.apache.felix.scr.annotations.Component;
 import org.apache.felix.scr.annotations.Properties;
 import org.apache.felix.scr.annotations.Property;
 import org.apache.felix.scr.annotations.Reference;
 import org.apache.felix.scr.annotations.Service;
 import org.apache.sling.api.SlingHttpServletRequest;
 import org.apache.sling.api.SlingHttpServletResponse;
 import org.apache.sling.api.servlets.SlingSafeMethodsServlet;
  
 import com.day.cq.commons.Externalizer;
  
 @Component(label = "acs PDF Generation Servlet", description = "ACS PDF Generation Servlet")
 @Service
 @Properties({@Property(name = "sling.servlet.resourceTypes", value = {"cq/Page"}),
 @Property(name = "sling.servlet.methods", value = {"GET"}),
 @Property(name = "service.description", value = "PDF Generation Servlet"),
 @Property(name = "service.vendor", value = "AGS"),
 @Property(name = "sling.servlet.selectors", value = {"pdf"})})
 public class PDFGenerationServlet extends SlingSafeMethodsServlet {
  
 /**
 *
 */
 private static final long serialVersionUID = 3537924656080668802L;
 @Reference
 private Externalizer externalizer;
 protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) {
 String htmlPagePath=request.getPathInfo();
 String fullLinkPath=!htmlPagePath.startsWith("http")?externalizer.publishLink(request.getResourceResolver(), htmlPagePath):htmlPagePath;
 ProcessBuilder pb = new ProcessBuilder("wkhtmltopdf.exe", fullLinkPath, "-");
 try {
 Process process = pb.start();
 response.setContentType("application/pdf");
 response.setHeader("Content-disposition", "attachment; filename=\"name.pdf\"");
  
 IOUtils.copy(process.getInputStream(), response.getOutputStream());
 process.waitFor();
 }
 catch (Exception e) {
 // // TODO Auto-generated catch block
 e.printStackTrace();
 // response.sendError(403);
 }
 }
  
 }
smacdonald2008
smacdonald2008Accepted solution
Level 10
October 16, 2015
Dterner
DternerAuthor
Level 2
October 16, 2015

@kalyanar Thanks! That is a very interesting approach I will look into this solution.