HTML to PDF / DOCX / RTF Java converter library › Forums › PD4ML v3 Archived Forums (Read Only) › General questions / FAQ › need servlet code for converting html string to pdf
- This topic has 8 replies, 3 voices, and was last updated Jun 23, 2014
08:28:34 by PD4ML.
-
AuthorPosts
-
June 15, 2012 at 07:09#26742
hello,
can you please provide me the servlet code in which i can convert html string url to pdf.it will save my lot of time.
Prashant
June 15, 2012 at 15:05#29032[language=java:2bbaexst]import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.URL;import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import org.zefer.pd4ml.PD4Constants;
import org.zefer.pd4ml.PD4ML;public class PD4MLServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
URL url = new URL(“http://pd4ml.com”);
PD4ML html = new PD4ML();
html.setPageSize( PD4Constants.A4 );
html.setPageInsets( new java.awt.Insets(20, 50, 10, 10) );
html.setHtmlWidth( 850 );ByteArrayOutputStream baos = new ByteArrayOutputStream();
html.render( url, baos );byte[] result = baos.toByteArray();
response.setContentType(“application/pdf”);
response.setHeader(“Content-disposition”,”inline; filename=test.pdf”);
response.setHeader(“Pragma”, “cache”);
response.setHeader(“Expires”, “0”);
response.setHeader(“Cache-control”, “private”);response.setContentLength(result.length);
ServletOutputStream sos = response.getOutputStream();
sos.write( result );
}
}[/language:2bbaexst]June 18, 2012 at 09:08#29033Hello,
I tried to run above code in my applicayion which is a JSF enabled application.while trying to convert given URL i get the following exception.java.lang.NullPointerException
org.zefer.pd4ml.PD4ML.o00000(Unknown Source)
org.zefer.pd4ml.PD4ML.render(Unknown Source)
com.xyz.report.jsf.HtmlToPDF.doGet(HtmlToPDF.java:38)
javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:198)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
com.xyz.servlet.BaseEntityFilter.doFilter(BaseEntityFilter.java:44)my code is
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
URL url = new URL(“http://localhost:8080/xyzweb/faces/templates/includes/report/A104_fv_1.xhtml”);
PD4ML pd4ml = new PD4ML();
pd4ml.setPageSize( PD4Constants.A4 );
pd4ml.setPageInsets( new java.awt.Insets(20, 50, 10, 10) );
pd4ml.setHtmlWidth(1000);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
pd4ml.render(url, baos);
//pd4ml.render(arg0, arg1)
byte[] result = baos.toByteArray();response.setContentType(“application/pdf”);
response.setHeader(“Content-disposition”,”inline; filename=test.pdf”);
response.setHeader(“Pragma”, “cache”);
response.setHeader(“Expires”, “0”);
response.setHeader(“Cache-control”, “private”);response.setContentLength(result.length);
ServletOutputStream sos = response.getOutputStream();
sos.write( result );
}June 18, 2012 at 15:04#29034Which PD4ML version do you currently use? You could check it in Manifest file of pd4ml.jar
June 19, 2012 at 03:46#29035Hello,
i am using pd4ml 3.8.0fx4 versionJune 19, 2012 at 04:03#29036i think the problem is url is not able to read because of session. the xhtml in url shown is generated through the code and then user can view the report through that url.
do you have any suggestions how to handle this case with the session?Prashant.
June 19, 2012 at 17:47#29037You need to pass the session id with request.
The simplest way would be to embed the session ID to the URL:
[language=java:3d15w8pg]URL url = new URL(“http://localhost:8080/xyzweb/faces/templates/includes/report/A104_fv_1.xhtml” +
“;jsessionid=” + request.getSession().getId());[/language:3d15w8pg]A bit trickier approach is to pass session IDs via cookies.
June 23, 2014 at 08:21#29038Hi,
I have been using the below sample code in my application. I have been using the demo jar.
PD4ML html = new PD4ML();<br /> html.setPageSize(new java.awt.Dimension(450, 450));<br /> html.setPageInsets(new java.awt.Insets(20, 50, 10, 10));<br /> html.setHtmlWidth(750);<br /> html.enableImgSplit(false);<br /> response.setHeader("Content-Disposition", "attachment; filename=test.pdf");<br /> <br /> URL base = new URL("file:D:/home.jsp");<br /> html.render(url, baos);<br />
Is this feature available in PD4ML library or in PD4Ml library PRO? I have to give this info to my procurement team.
June 23, 2014 at 08:28#29039The code works with both Std and Pro versions.
The Pro version would also allow you to embed TTF fonts and to display with them non-Latin and special characters.
-
AuthorPosts
The forum ‘General questions / FAQ’ is closed to new topics and replies.