Fórum

Print version and PDF-Display

Morad Ahmad, modificado 7 Anos atrás.

Print version and PDF-Display

Junior Member Postagens: 71 Data de Entrada: 16/06/10 Postagens Recentes
Hi,

to provide a printer friendly display of an event-list I use a jsp like these

<liferay-portlet:renderURL var="printURL" windowState="<%= LiferayWindowState.EXCLUSIVE.toString() %>">
<portlet:param name="mvcPath" value="/html/eventslist/print/print.jsp" />
<portlet:param name="pStartDate" value="<%= filterFromDate %>" />
<portlet:param name="pEndDate" value="<%= filterToDate %>" />
</liferay-portlet:renderURL>

These works fine.

Now I want to provide a PDF-Version to the users to simplify loading the events as PDF directly (avoid print > save as pdf...). To do that I want to use the same jsp generating the printer friendly html.

I solved these threw passing the printURL to the "generatePDF" Methode in the Portlet:

<liferay-portlet:resourceURL var="pdfURL" id="createPDF">
<portlet:param name="printURL" value="<%= printURL.toString() %>" />
</liferay-portlet:resourceURL>

<div style="display: block; margin: 8px 4px 8px 0; text-align: right; clear: both; width: 98%;">
<a class="pdf-button" href="<%= pdfURL.toString() %>" target="_blank" ><i class="fa fa-file-pdf-o" aria-hidden="true"></i></a>
<a class="print-button" href="<%= printURL.toString() %>" target="_blank" ><i class="fa fa-print" aria-hidden="true"></i></a>
</div>

Portlet-Class:

private void createPDF(ResourceRequest resourceRequest, ResourceResponse resourceResponse) throws IOException {
ThemeDisplay themeDisplay = (ThemeDisplay) resourceRequest.getAttribute(WebKeys.THEME_DISPLAY);
String printURL = ParamUtil.getString(resourceRequest, "printURL");
System.out.println("printURL= " + printURL);
byte[] pdf = PortangoPDFUtils.generatePDF(themeDisplay, printURL);
resourceResponse.setContentType("application/pdf");
OutputStream out = resourceResponse.getPortletOutputStream();
out.write(pdf);
out.flush();
}

PortangoPDFUtils (uses openpdftohtml) works fine and generates very nice pdf:

public static byte[] generatePDF(ThemeDisplay themeDisplay, String printURL) {
try {
ByteArrayOutputStream out = new ByteArrayOutputStream();
exportToPdfBox(printURL, out);
return out.toByteArray();
} catch (Exception e) {
_log.error(e);
return null;
}
}

public static void exportToPdfBox(String url, OutputStream os) throws Exception {
PdfRendererBuilder builder = createPdfRendererBuilder();
org.w3c.dom.Document dom = html5ParseDocument(url, 10000);
builder.withW3cDocument(dom, url);
builder.toStream(os);
builder.run();
}

private static PdfRendererBuilder createPdfRendererBuilder() {
PdfRendererBuilder builder = new PdfRendererBuilder();
builder = builder.useDefaultPageSize(210, 290, PdfRendererBuilder.PageSizeUnits.MM);
return builder;
}
public static org.w3c.dom.Document html5ParseDocument(String urlStr, int timeoutMs) throws IOException {
URL url = new URL(urlStr);
org.jsoup.nodes.Document doc;
if (url.getProtocol().equalsIgnoreCase("file")) {
doc = Jsoup.parse(new File(url.getPath()), "UTF-8");
} else {
doc = Jsoup.parse(url, timeoutMs);
}
return DOMBuilder.jsoup2DOM(doc);
}

My solution works will, however I search for a solution to avoid calling the "printURL" as an URL (call portal methods 2 times).
Instead I search for a way using the print.jsp to generate the html in a string, then the pdf to serve...

Thanks,
Morad.