Expand my Community achievements bar.

SOLVED

Using the PDF rewriter with custom xml output

Avatar

Level 2

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?

1 Accepted Solution

Avatar

Correct answer by
Level 10
7 Replies

Avatar

Employee

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

Avatar

Level 2

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.

Avatar

Level 2

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.

Avatar

Employee

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);
 }
 }
  
 }

Avatar

Correct answer by
Level 10

Avatar

Level 2

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