Expand my Community achievements bar.

Enhance your AEM Assets & Boost Your Development: [AEM Gems | June 19, 2024] Improving the Developer Experience with New APIs and Events
SOLVED

How to export a page content in HTML format?

Avatar

Level 2

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.

1 Accepted Solution

Avatar

Correct answer by
Level 4

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 

View solution in original post

3 Replies

Avatar

Correct answer by
Level 4

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 

Avatar

Employee Advisor

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