HTML to PDF / DOCX / RTF Java converter library › Forums › PD4ML v3 Archived Forums (Read Only) › HTML/CSS rendering issues › Error while rendering PDF › Re: Re: Error while rendering PDF
hello
here is an example servlet that uses the class directly beneath it to do the conversion job
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package ae.rwd.servlet;
import ae.rwd.model.Pdf;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
*
* @author Anas
*/
public class PdfCreater extends HttpServlet {
private static final String bodyUrlParamater = “bodyUrl”;
private static final String headerUrlParamater = “headerUrl”;
private static final String footerUrlParamater = “footerUrl”;
private static final String charsetParamater = “charset”;
private static final String contentType = “application/pdf”;
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
ServletOutputStream out = response.getOutputStream();
String bodyUrl = null;
String headerUrl = null;
String footerUrl = null;
String charset = null;
bodyUrl = request.getParameter(PdfCreater.bodyUrlParamater);
if (bodyUrl == null || bodyUrl.length() == 0) {
throw new ServletException();
}
headerUrl = request.getParameter(PdfCreater.headerUrlParamater);
footerUrl = request.getParameter(PdfCreater.footerUrlParamater);
charset = request.getParameter(PdfCreater.charsetParamater);
ByteArrayOutputStream baos = Pdf.convertHtmlToPdf(bodyUrl, headerUrl, footerUrl, charset);
response.setContentType(PdfCreater.contentType);
response.setContentLength(baos.size());
out = response.getOutputStream();
baos.writeTo(out);
out.flush();
out.close();
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
public String getServletInfo() {
return “Short description”;
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package ae.rwd.model;
import java.awt.Dimension;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.net.MalformedURLException;
import java.net.URL;
import org.zefer.pd4ml.PD4Constants;
import org.zefer.pd4ml.PD4ML;
import org.zefer.pd4ml.PD4PageMark;
/**
*
* @author Anas
*/
public class Pdf {
protected static int userSpaceWidth = 638;
protected static Dimension format = PD4Constants.A4;
protected static boolean landscapeValue = false;
protected static int topValue = 10;
protected static int leftValue = 50;
protected static int rightValue = 50;
protected static int bottomValue = 10;
public static ByteArrayOutputStream convertHtmlToPdf(String bodyUrl, String headerUrl, String footerUrl, String charsetParam) throws MalformedURLException, FileNotFoundException, IOException {
java.io.ByteArrayOutputStream fos = null;
PD4ML pd4ml = null;
PD4PageMark header = null;
PD4PageMark footer = null;
String charset = “windows-1256”;
URL url = new URL(bodyUrl);
fos = new java.io.ByteArrayOutputStream();
pd4ml = new PD4ML();
if (charsetParam != null && charsetParam.trim().length() > 0 && charsetParam.equalsIgnoreCase(“UTF-8”)) {
charset = “UTF-8”;
}
if (headerUrl != null) {
header = new PD4PageMark();
header.setHtmlTemplate(getUrlAsStream(headerUrl, charset));
header.setAreaHeight(-1);
pd4ml.setPageHeader(header);
}
if (footerUrl != null) {
footer = new PD4PageMark();
footer.setHtmlTemplate(getUrlAsStream(footerUrl, charset));
footer.setAreaHeight(-1);
pd4ml.setPageFooter(footer);
}
pd4ml.useTTF(“java:fonts”, true);
//pd4ml.useTTF(“/windows/fonts”, true);
//pd4ml.setDefaultTTFs(“Times New Roman”, “Arial”, “Courier New”);
//pd4ml.setDefaultTTFs(“Simplified Arabic”, “ae_Arab”, “ae_Nice”);
pd4ml.setPermissions(“empty”, PD4ML.AllowCopy, false);
pd4ml.setPermissions(“empty”, PD4ML.AllowModify, false);
pd4ml.setPermissions(“empty”, PD4ML.AllowPrint, true);
// pd4ml.setPageInsets(new Insets(topValue, leftValue, bottomValue, rightValue));
pd4ml.setHtmlWidth(userSpaceWidth);
pd4ml.setPageSize(format);
pd4ml.enableDebugInfo();
pd4ml.adjustHtmlWidth();
pd4ml.render(url, fos);
return fos;
}
private static String getUrlAsStream(String Url, String charset) throws MalformedURLException, IOException {
URL url = new URL(Url);
return convertInputStreamToString(url.openConnection().getInputStream(), charset);
}
private static String convertInputStreamToString(InputStream is, String charset) throws IOException, UnsupportedEncodingException {
if (is != null) {
Writer writer = new StringWriter();
char[] buffer = new char[1024];
try {
Reader reader = new BufferedReader(new InputStreamReader(is, charset));
int n;
while ((n = reader.read(buffer)) != -1) {
writer.write(buffer, 0, n);
}
} finally {
is.close();
}
return writer.toString();
} else {
return “”;
}
}
}
i hope this good for you
thanks