Converting HTML to PDF

Post any questions you have about using the Verj.io Studio, including client and server-side programming with Javascript or FPL, and integration with databases, web services etc.

Moderators: Jon, Steve, Ian, Dave

Vircos
Ebase User
Posts: 97
Joined: Thu Sep 13, 2007 6:07 am
Location: The Netherlands

Converting HTML to PDF

#1

Postby Vircos » Mon Aug 24, 2009 1:45 pm

First of all I am not a Java programmer but I gave it a try. I want to convert xhtml to a PDF. This way I am not limited to static PDF's used by Ebase for the print resources. I searched the internet and came along a java library which should be able to do the trick: xhtmlrenderer (https://xhtmlrenderer.dev.java.net/). This library uses the itext library which is also used by Ebase.

At the moment I have done the following steps:

1.
Downloaded the xhtmlrenderer library and placed it in webapps/ufs/WEB-INF/lib

2. Created a new folder in webapps/ufs/WEB-INF/classes called formservlets and placed the file Html2Pdf.class in this folder. This is a servlet that should convert a html string parsed by a post-request to a PDF.

3.
Placed the file ConvertHtml2Pdf.class in the customfunctions directory

4. Added the following to the web.xml

Code: Select all

      <filter-mapping>
        <filter-name>EbaseFilter</filter-name>
        <url-pattern>/Html2Pdf</url-pattern>
      </filter-mapping>	  

Code: Select all

      <servlet>
         <servlet-name>Html2Pdf</servlet-name>
         <servlet-class>formservlets.Html2Pdf</servlet-class>
      </servlet>	

Code: Select all

      <servlet-mapping>
         <servlet-name>Html2Pdf</servlet-name>
         <url-pattern>/Html2Pdf</url-pattern>
      </servlet-mapping>	
5. Restarted the server

6. Added customfunction with "Maintain functions" within Ebase Designer.
The custom functions should parse the following two paramteres: "URL to CSS", "HTML to be placed in body of HTML document". The customfunction should create a popup which calls the Html2Pdf servlet.

My hope was that this would trick. But unfortunately it did not. The log does not show any errors. I hope someone here is able to help me with this.

Customfunction - ConvertHtml2Pdf.java

Code: Select all

package com.ebasetech.ufs.customfunctions;

import java.util.Stack;

import org.nfunk.jep.ParseException;

import com.ebasetech.ufs.function.UFSCustomFunction;
import com.ebasetech.ufs.kernel.BrowserPopUp;
import com.ebasetech.ufs.kernel.Form;
import com.ebasetech.ufs.kernel.FormException;
import com.ebasetech.ufs.kernel.UFSFormInterface;
import com.ebasetech.ufs.utility.HttpUtil;


public class ConvertHtml2Pdf extends UFSCustomFunction &#123;
	
	public ConvertHtml2Pdf&#40;UFSFormInterface formInterface&#41; &#123;
		super&#40;formInterface&#41;;
		numberOfParameters = 2;
	&#125;
	
	public void run&#40;Stack inStack&#41; throws FormException, ParseException &#123;
		
		checkStack&#40;inStack&#41;;
		
	    Object param2 = inStack.pop&#40;&#41;;
	    Object param1 = inStack.pop&#40;&#41;;
	    
	    if &#40;param1 instanceof String && param2 instanceof String&#41; &#123;
		    String css = param1.toString&#40;&#41;;
		    String html = param2.toString&#40;&#41;;
		    
		    String content = "<html>" +
		    			"<head>" +
		    			"<link rel='stylesheet' type='text/css'" +
		    			"href='" + css + "' media='print' />" +
		    			"</head>" +
		    			"<body>" + html + "</body></html>";
		    
		    Form form = new Form&#40;&#41;;
			form.setPopup&#40;new BrowserPopUp&#40;HttpUtil.encodeURL&#40;"Html2Pdf?content=" + content&#41;&#41;&#41;;
			inStack.push&#40;"OK"&#41;;
	    &#125; else &#123;
	    	 throw new ParseException&#40;"Invalid parameter type"&#41;;
	    &#125;
	&#125;
	
	
&#125;

Formservlet - Html2Pdf.java

Code: Select all

package formservlets;

import com.ebasetech.ufs.kernel.*;
import java.io.*;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.xhtmlrenderer.pdf.ITextRenderer;
import org.w3c.dom.Document;

@SuppressWarnings&#40;"serial"&#41;
public class Html2Pdf extends HttpServlet &#123;
	
	public Html2Pdf&#40;&#41; &#123;
		
	&#125;

	private void displayError&#40;HttpServletRequest request,
			HttpServletResponse response, String errorMessage&#41;
			throws ServletException, IOException &#123;
		PrintWriter out = response.getWriter&#40;&#41;;
		out.println&#40;"<HTML>"&#41;;
		out.println&#40;" <HEAD>"&#41;;
		out.println&#40;"<TITLE>Error notification page</TITLE>"&#41;;
		out
				.println&#40;"<META http-equiv=\"Content-Type\" content=\"text/html; charset=ISO-8859-1\">"&#41;;
		out.println&#40;"<META HTTP-EQUIV=\"Pragma\" CONTENT=\"no-cache\">"&#41;;
		out.println&#40;"<META HTTP-EQUIV=\"Cache-Control\" CONTENT=\"no-cache\">"&#41;;
		out.println&#40;"<META HTTP-EQUIV=\"Expires\" CONTENT=\"0\">"&#41;;
		out.println&#40;" </HEAD>"&#41;;
		out.println&#40;"<BODY>"&#41;;
		out.println&#40;&#40;new StringBuilder&#40;"Unexpected error in HTML to PDF conversion&#58; "&#41;&#41;
				.append&#40;errorMessage&#41;.toString&#40;&#41;&#41;;
		out.println&#40;"</BODY>"&#41;;
		out.println&#40;"</HTML>"&#41;;
		out.close&#40;&#41;;
	&#125;

	public void doGet&#40;HttpServletRequest request, HttpServletResponse response&#41;
			throws ServletException, IOException &#123;
		processRequest&#40;request, response&#41;;
	&#125;

	public void doPost&#40;HttpServletRequest request, HttpServletResponse response&#41;
			throws ServletException, IOException &#123;
		processRequest&#40;request, response&#41;;
	&#125;

	@SuppressWarnings&#40;"deprecation"&#41;
	public void processRequest&#40;HttpServletRequest request,
			HttpServletResponse response&#41; throws ServletException, IOException &#123;
		FormStack formStack = EbaseFormSession.getFormSession&#40;request&#41;
				.getFormStack&#40;&#41;;
		Form form = null;
		if &#40;formStack != null&#41; &#123;
			form = formStack.peekForm&#40;&#41;;
		&#125;
		if &#40;form == null&#41; &#123;
			displayError&#40;request, response, "No Ebase form active"&#41;;
			return;
		&#125;
        response.setContentType&#40;"application/pdf"&#41;;
        
        String content = request.getParameter&#40;"content"&#41;;
        
        StringBuffer buf = new StringBuffer&#40;&#41;;
        buf.append&#40;content&#41;;
        
        try &#123;
            DocumentBuilder builder = DocumentBuilderFactory.newInstance&#40;&#41;.newDocumentBuilder&#40;&#41;;
            Document doc = builder.parse&#40;new StringBufferInputStream&#40;buf.toString&#40;&#41;&#41;&#41;;
            ITextRenderer renderer = new ITextRenderer&#40;&#41;;
            renderer.setDocument&#40;doc, null&#41;;
            renderer.layout&#40;&#41;;
            OutputStream os = response.getOutputStream&#40;&#41;;
            renderer.createPDF&#40;os&#41;;
            os.close&#40;&#41;;
        &#125; catch &#40;Exception e&#41; &#123;
        	throw new ServletException&#40;e.getMessage&#40;&#41;, e&#41;;
        &#125;

	&#125;
&#125;
0 x
What's the meaning of Justice...

User avatar
Joost
Ebase User
Posts: 49
Joined: Fri Sep 14, 2007 6:14 pm
Location: The Netherlands

Re: Converting HTML to PDF

#2

Postby Joost » Thu Aug 27, 2009 11:31 am

Vircos wrote:3.[/b] Placed the file ConvertHtml2Pdf.class in the customfunctions directory
Do you mean the folder: webapps/ufs/WEB-INF/classes/com/ebasetech/ufs/customfunctions?

Everything else you did/wrote seems ok. I haven't tried your code, so it could be something else is the matter.
0 x

Vircos
Ebase User
Posts: 97
Joined: Thu Sep 13, 2007 6:07 am
Location: The Netherlands

Re: Converting HTML to PDF

#3

Postby Vircos » Thu Aug 27, 2009 12:21 pm

Joost wrote:
Vircos wrote:3.[/b] Placed the file ConvertHtml2Pdf.class in the customfunctions directory
Do you mean the folder: webapps/ufs/WEB-INF/classes/com/ebasetech/ufs/customfunctions?

Everything else you did/wrote seems ok. I haven't tried your code, so it could be something else is the matter.
That is what I mean.
I suspect my code is the problem. As I said before: I am not an experienced programmer so it is a guess.
0 x
What's the meaning of Justice...

User avatar
Joost
Ebase User
Posts: 49
Joined: Fri Sep 14, 2007 6:14 pm
Location: The Netherlands

#4

Postby Joost » Fri Aug 28, 2009 11:16 am

I just thought of another thing. The print pdf popup doesn't work in the after form event, at least it doesn't show the popup. If you are executing your custom function in that event as well, that could be the reason.
0 x

Vircos
Ebase User
Posts: 97
Joined: Thu Sep 13, 2007 6:07 am
Location: The Netherlands

#5

Postby Vircos » Fri Aug 28, 2009 2:22 pm

Joost wrote:I just thought of another thing. The print pdf popup doesn't work in the after form event, at least it doesn't show the popup. If you are executing your custom function in that event as well, that could be the reason.
I dit not know that. But in my case I use a button with an onclick event.
0 x
What's the meaning of Justice...

User avatar
Wai
Moderator
Moderator
Posts: 165
Joined: Wed Sep 12, 2007 9:04 am
Location: Sandy, UK
Contact:

#6

Postby Wai » Wed Sep 02, 2009 1:24 pm

You could bypass the custom function altogether and use the Ebase display command to call the servlet passing any parameters in the URL to the servlet.

The display command will open the response from the servlet in the popup window.
0 x

Vircos
Ebase User
Posts: 97
Joined: Thu Sep 13, 2007 6:07 am
Location: The Netherlands

#7

Postby Vircos » Mon Sep 14, 2009 2:01 pm

Wai wrote:You could bypass the custom function altogether and use the Ebase display command to call the servlet passing any parameters in the URL to the servlet.

The display command will open the response from the servlet in the popup window.
I did not thought about that. It should work but it will limit the amound of characters that can be passed through with the HTML-GET-request (passing parameters in the URL). In this case I prefer a POST-request as I do not need the URL for passing parameters.
0 x
What's the meaning of Justice...

User avatar
Wai
Moderator
Moderator
Posts: 165
Joined: Wed Sep 12, 2007 9:04 am
Location: Sandy, UK
Contact:

#8

Postby Wai » Mon Sep 14, 2009 2:19 pm

That would work, or store the html / css etc in database table, then only pass the ID to the servlet.
0 x


Who is online

Users browsing this forum: No registered users and 117 guests