How to export a page content in HTML format? | Community
Skip to main content
New Participant
January 10, 2023
Solved

How to export a page content in HTML format?

  • January 10, 2023
  • 3 replies
  • 1116 views

I have a requirement to export a page content including the styling in HTML format so that can be exported to third party. 

 

We are not using GraphQL as we need the styling as well to be exported.

 

Or, if we can export the page content including styling in JSON format so that the third party can render it on their system.

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 Harishv31

Hi ,

 

if you want to read page html response in Java, Please check this below code.

 

 

import org.apache.sling.engine.SlingRequestProcessor;
import com.day.cq.contentsync.handler.util.RequestResponseFactory;

@Reference
private RequestResponseFactory requestResponseFactory;

@Reference
private SlingRequestProcessor requestProcessor;

public String doStuff(){
    HttpServletRequest request = requestResponseFactory.createRequest("GET", "/path/to/your/node.html");
    request.setAttribute(WCMMode.REQUEST_ATTRIBUTE_NAME, WCMMode.DISABLED);

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    HttpServletResponse response = requestResponseFactory.createResponse(out);

    requestProcessor.processRequest(request, response, resourceResolver);        
    return out.toString(response.getCharacterEncoding());
}

 

Thanks 

3 replies

Mohit_KBansal
Employee
January 10, 2023

I suggest using the selector approach here. In your template, implement a selector which will render styled HTML only.

 

Your 3rd parties can use a page path with selector like www.example.com/path/of/page.content.html

Harishv31Accepted solution
New Participant
January 10, 2023

Hi ,

 

if you want to read page html response in Java, Please check this below code.

 

 

import org.apache.sling.engine.SlingRequestProcessor;
import com.day.cq.contentsync.handler.util.RequestResponseFactory;

@Reference
private RequestResponseFactory requestResponseFactory;

@Reference
private SlingRequestProcessor requestProcessor;

public String doStuff(){
    HttpServletRequest request = requestResponseFactory.createRequest("GET", "/path/to/your/node.html");
    request.setAttribute(WCMMode.REQUEST_ATTRIBUTE_NAME, WCMMode.DISABLED);

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    HttpServletResponse response = requestResponseFactory.createResponse(out);

    requestProcessor.processRequest(request, response, resourceResolver);        
    return out.toString(response.getCharacterEncoding());
}

 

Thanks