com.isomorphic.contentexport
Class PdfExport

com.isomorphic.contentexport.PdfExport
All Implemented Interfaces:
IPdfExporter

public class PdfExport
implements IPdfExporter


Method Summary
 void getPdfObject(java.lang.String html, java.util.Map settings, javax.servlet.http.HttpServletResponse response, RPCManager rpc)
          Converts the provided HTML to a PDF object and streams the response back to the browser as the response to an RPCRequest/DSRequest.
 org.xhtmlrenderer.pdf.ITextRenderer getPdfRenderer(java.lang.String html, java.util.Map settings)
          Method to create an ITextRenderer object ready to generate a PDF object.
 

Method Detail

getPdfObject

public void getPdfObject(java.lang.String html,
                         java.util.Map settings,
                         javax.servlet.http.HttpServletResponse response,
                         RPCManager rpc)
                  throws java.lang.Exception
Converts the provided HTML to a PDF object and streams the response back to the browser as the response to an RPCRequest/DSRequest.

For additional methods, including the ability to generate a .pdf that be saved to a database or emailed rather than streamed to the client, see PdfExport.getPdfRenderer(String, Map)

When the provided HTML contains <img> tags, such as when an Img widget is to be included in the PDF export, SmartClient Server Framework needs to be able to download the image for inclusion in the PDF. If images are not appearing in an exported PDF, examine the server's log output for information on why an image request failed. Common ResourceLoader errors include:

Specified by:
getPdfObject in interface IPdfExporter
Parameters:
html - generated html from client-side
settings - map of parameters
response - in this parameter we send the answer
rpc - our RPCManager parameter
Throws:
java.lang.Exception

getPdfRenderer

public org.xhtmlrenderer.pdf.ITextRenderer getPdfRenderer(java.lang.String html,
                                                          java.util.Map settings)
                                                   throws java.lang.Exception
Method to create an ITextRenderer object ready to generate a PDF object. With the ITextRenderer object, you can use any OutputStream object to generate a PDF object, eg.: To generate a PDF file from this ITextRenderer object, you can make:
server-side:
 public String generatePdf(DSRequest request) {
     String response = "Operation completed successfully";
     try {
         Map data = request.getValues();
         Map settings = new HashMap();
         settings.put("skinName", data.get("skinName").toString());
         String dirServer = "/shared/directoryPdf/";
         String fileName = "default";
         String outputFile = dirServer+fileName+".pdf";
         OutputStream outputSream = new FileOutputStream(outputFile);
         PdfExport export = new PdfExport();
         ITextRenderer renderer = export.getPdfRenderer(data.get("html").toString(), settings);
         renderer.createPDF(outputSream);
         outputSream.close();
     }catch (Exception e) {
         e.printStackTrace();
         response = e.getMessage();
     }
     return response;
 }
 
You can use DataSource.performCustomOperation() in client-side to invoke your custom method in server-side, eg.:
 ...........
 vLayout.getPrintHTML(null, new PrintHTMLCallback(){
     public void setHTML(String html) {
         DSRequest request = new DSRequest();
         LinkedHashMap data = new LinkedHashMap();
         data.put("html", html);
         data.put("skinName", "Enterprise");
         request.setData(data);
         ds.performCustomOperation("generatePdf", new Record(), new DSCallback() {
             public void execute(DSResponse response, Object rawData, DSRequest request) {
                 SC.say((String)rawData);
             }
         }, request);
     }
 });
 
and the .ds.xml DataSource would be something like this:
 <DataSource ID="supplyItem>
 <fields>
     ..........
 </fields>
 <operationBindings>
     <operationBinding operationId="generatePdf" operationType="custom" serverMethod="generatePdf">
         <serverObject lookupStyle="new" className="com.smartgwt.sample.server.CustomOperation"/>
     </operationBinding>
 </operationBindings>
 </DataSource>
 
 

If required, it is possible to use an extra stylesheet. To make the extra stylesheet available for this process, follow these steps:
1.- In server.properties, set:
skin.[skinName].extraStyleSheet: path/to/stylesheet/[extraStyleSheet.css]
Eg.: skin.Enterprise.extraStyleSheet: dirSkins/mySkins/myStyleSheet.css
The dirSkins/ folder must be placed under webroot.
2.- When using images within the extra stylesheet, these images must be placed in, for example:
- dirSkins/mySkins/images/picture1.png (as in the previous example)
Or, when these images are in a different folder other than the extra stylesheet:
- myCustomImages/48x48/picture1.png (The myCustomImages/ folder must be placed under webroot)

- Internationalization and Fonts with PDFs and iText

When you are displaying HTML content in the browser, and you use non-Latin Unicode characters such as Japanese characters or Cyrillic letters, web browsers will automatically locate and use a font that includes these characters.

PDF generation doesn't work this way. When you declare in CSS that text content in your HTML is rendered with a certain font-family, that font-family is what iText will use, even if the font file for that font-family has no support for some of the characters in your text. Unfortunately, the default font files included with iText for Arial and other common web fonts do not include support for many non-Latin characters, including Cyrillic and CJK (Chinese Japanese Korean) characters.

To get around this problem, you need to provide a font file that has support for international characters, and explicitly tell iText to use it. For example, the ARIALUNI.TFF file that exists in standard Windows installations has a font-family "Arial Unicode MS" with support for all Unicode 2.0 characters.

To use this file, you would provide an extraStyleSheet with a declaration like this:

Parameters:
html - generated html from client-side
settings - map of parameters
Returns:
ITextRenderer
Throws:
java.lang.Exception