com.secondmove.webpdfreports
Class HTML2PDFConvertor

java.lang.Object
  extended bycom.secondmove.webpdfreports.HTML2PDFConvertor

public class HTML2PDFConvertor
extends java.lang.Object

Version:
1.0a

HTMP2PDFConvertor is the class that allows you to create PDf documents either from a given URL or from an HTML string and a baseURL. The class greatly simplifies constructing PDF documents from HTML documents. The following codesnippet produces a multi-page PDF Document and opens the resulting document using the systems preferred PDF application. Simply pass the URL's of the pages to be contained in the document as argument to the tool.

 public static void main (String args[]) {
         if (args.length > 0) {
                 HTMP2PDFConvertor pdfConvertor = new HTMP2PDFConvertor();
                 pdfConvertor.createDocument("A4", PortraitOrientation);

                 for (int i = 0; i < args.length; i++) {
                         System.out.println("Processing " + args[i]);
                         pdfConvertor.appendPageWithURL(args[i]);
                 }
                 String name = pdfConvertor.closeDocument();
                 Runtime rt = java.lang.Runtime.getRuntime();
                 try {
                         rt.exec("open file:///" + name);
                 } catch (java.lang.Throwable t) {
                         System.out.println("Error opening file:" + t);
                 }
         }
 }
 
Author:
Jan Willem Luiten, Second Move vof the Netherlands

Field Summary
static int AutoPagination
          Documentation forthcoming
static int ClipPagination
          Documentation forthcoming
static int FitPagination
          Documentation forthcoming
static int LandscapeOrientation
          Orientation is landscape (page is wider than it is tall).
static int PortraitOrientation
          Orientation is portrait (page is taller than it is wide).
 
Constructor Summary
HTML2PDFConvertor()
           
 
Method Summary
 void appendPageWithHTMLStringAndBaseURL(java.lang.String htmlString, java.lang.String baseURL)
          Appends a page to the PDF document converting HTML to pdf
 void appendPageWithURL(java.lang.String url)
          Appends a page to the PDF document converting HTML to pdf
 float bottomMargin()
          Returns the bottom margin in points.
 java.lang.String closeDocument()
          Closes the PDF document
 void createDocument()
          Creates a PDF document using "A4" as the papername and PortraitOrientation as the orientation.
 void createDocument(PaperSize size, int orientation)
          Creates a PDF document.
 void createDocument(java.lang.String paperName, int orientation)
          Creates a pdf document.
 int horizontalPagination()
          Returns the horizontal pagination mode.
 float leftMargin()
          Returns the left margin in points.
 int orientation()
          Returns the orientation of the document.
 java.lang.String paperName()
          Returns the papername currently in use by this document.
 PaperSize paperSize()
          Returns the papersize in points.
 float printScalingFactor()
          Returns the scale factor percentage.
 boolean printsHorizontallyCentered()
          Retrieves the horizontal centration mode.
 boolean printsVerticallyCentered()
          Retrieves the vertical centration mode.
 float rightMargin()
          Returns the right margin in points.
 void setBottomMargin(float margin)
          Sets the bottom margin.
 void setHorizontalPagination(int paginationMode)
          Sets the horizontal pagination mode.
 void setLeftMargin(float margin)
          Sets the left margin.
 void setOrientation(int orientation)
          Sets the orientation for the document.
 void setPaperName(java.lang.String name)
          Sets the papername for this document Invocation only affects pages yet to be appended, not the pages already appended.
 void setPaperSize(PaperSize size)
          Sets the papersize for the document.
 void setPrintScalingFactor(float factor)
          Sets the scale factor percentage before pagination.
 void setPrintsHorizontallyCentered(boolean centered)
          Sets the horizontal centration.
 void setPrintsVerticallyCentered(boolean centered)
          Sets the vertical centration.
 void setRightMargin(float margin)
          Sets the right margin.
 void setShouldPrintBackgrounds(boolean printBackgrounds)
          Determines wether or not to print backgrounds
 void setTopMargin(float margin)
          Sets the top margin.
 void setVerticalPagination(int paginationMode)
          Sets the vertical pagination mode.
 boolean shouldPrintBackgrounds()
          Returns wether or not backgrounds are printed, true if backgrounds are printed, false otherwise.
 float topMargin()
          Returns the top margin in points.
 int verticalPagination()
          Returns the vertical pagination mode.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

PortraitOrientation

public static final int PortraitOrientation
Orientation is portrait (page is taller than it is wide).

See Also:
orientation(), setOrientation(int), createDocument(), createDocument(String, int), Constant Field Values

LandscapeOrientation

public static final int LandscapeOrientation
Orientation is landscape (page is wider than it is tall).

See Also:
orientation(), setOrientation(int), createDocument(), createDocument(String, int), Constant Field Values

AutoPagination

public static final int AutoPagination
Documentation forthcoming

See Also:
Constant Field Values

FitPagination

public static final int FitPagination
Documentation forthcoming

See Also:
Constant Field Values

ClipPagination

public static final int ClipPagination
Documentation forthcoming

See Also:
Constant Field Values
Constructor Detail

HTML2PDFConvertor

public HTML2PDFConvertor()
Method Detail

createDocument

public void createDocument()
Creates a PDF document using "A4" as the papername and PortraitOrientation as the orientation.

See Also:
createDocument(String, int), createDocument(PaperSize, int), PortraitOrientation, LandscapeOrientation

createDocument

public void createDocument(java.lang.String paperName,
                           int orientation)
Creates a pdf document.

Parameters:
paperName - Name of the papertype used for the document, e.g. "A4", "US Legal", "US Letter" etc.
orientation - Determines the orientation of the document, either PortraitOrientation or LandscapeOrientation.
See Also:
createDocument(), PortraitOrientation, LandscapeOrientation

createDocument

public void createDocument(PaperSize size,
                           int orientation)
Creates a PDF document.

Parameters:
size - The PaperSize in points
orientation - The orientation, either LandscapeOrientation or PortraitOrientation
See Also:
PaperSize.PaperSize(float, float), PortraitOrientation, LandscapeOrientation

closeDocument

public java.lang.String closeDocument()
Closes the PDF document

Returns:
the pathname of the document.

setPrintsHorizontallyCentered

public void setPrintsHorizontallyCentered(boolean centered)
Sets the horizontal centration.

Parameters:
centered - If true, pages are centered horizontally
See Also:
printsHorizontallyCentered(), setPrintsVerticallyCentered(boolean), printsVerticallyCentered()

printsHorizontallyCentered

public boolean printsHorizontallyCentered()
Retrieves the horizontal centration mode.

Returns:
The horizontal centration, true if pages are horizontally centered, false otherwise.
See Also:
setPrintsHorizontallyCentered(boolean), setPrintsVerticallyCentered(boolean), printsVerticallyCentered()

setPrintsVerticallyCentered

public void setPrintsVerticallyCentered(boolean centered)
Sets the vertical centration.

Parameters:
centered - If true, pages are centered vertically
See Also:
setPrintsHorizontallyCentered(boolean), printsHorizontallyCentered(), printsVerticallyCentered()

printsVerticallyCentered

public boolean printsVerticallyCentered()
Retrieves the vertical centration mode.

Returns:
The vertical centration, true if pages are vertically centered, false otherwise.
See Also:
setPrintsHorizontallyCentered(boolean), printsHorizontallyCentered(), setPrintsVerticallyCentered(boolean)

setHorizontalPagination

public void setHorizontalPagination(int paginationMode)
Sets the horizontal pagination mode. Invocation only affects pages yet to be appended, not the pages already appended.

See Also:
horizontalPagination(), AutoPagination, ClipPagination, FitPagination

horizontalPagination

public int horizontalPagination()
Returns the horizontal pagination mode.

See Also:
setHorizontalPagination(int), AutoPagination, ClipPagination, FitPagination

setVerticalPagination

public void setVerticalPagination(int paginationMode)
Sets the vertical pagination mode. Invocation only affects pages yet to be appended, not the pages already appended.

See Also:
verticalPagination(), AutoPagination, ClipPagination, FitPagination

verticalPagination

public int verticalPagination()
Returns the vertical pagination mode.

See Also:
setVerticalPagination(int), AutoPagination, ClipPagination, FitPagination

setPrintScalingFactor

public void setPrintScalingFactor(float factor)
Sets the scale factor percentage before pagination. Invocation only affects pages yet to be appended, not the pages already appended.

Parameters:
factor - The scale factor percentage
See Also:
printScalingFactor()

printScalingFactor

public float printScalingFactor()
Returns the scale factor percentage.


setOrientation

public void setOrientation(int orientation)
Sets the orientation for the document. Invocation only affects pages yet to be appended, not the pages already appended.

Parameters:
orientation - The orientaion for the document, either PortraitOrientation (taller than wide) or LandscapeOrientation (wider than tall).
See Also:
orientation(), PortraitOrientation, LandscapeOrientation

orientation

public int orientation()
Returns the orientation of the document. Possible values are PortraitOrientationor or LandscapeOrientation.

See Also:
setOrientation(int), PortraitOrientation, LandscapeOrientation

setPaperSize

public void setPaperSize(PaperSize size)
Sets the papersize for the document. Invocation only affects pages yet to be appended, not the pages already appended.

Parameters:
size - The papersize in points.
See Also:
PaperSize.PaperSize(float, float)

paperSize

public PaperSize paperSize()
Returns the papersize in points.

See Also:
PaperSize.PaperSize(float, float)

setPaperName

public void setPaperName(java.lang.String name)
Sets the papername for this document Invocation only affects pages yet to be appended, not the pages already appended.

Parameters:
name - The papername to be used, e.g. "A4".

paperName

public java.lang.String paperName()
Returns the papername currently in use by this document.

See Also:
setPaperName(String)

setLeftMargin

public void setLeftMargin(float margin)
Sets the left margin. Invocation only affects pages yet to be appended, not the pages already appended.

Parameters:
margin - The left margin in points.

leftMargin

public float leftMargin()
Returns the left margin in points.


setRightMargin

public void setRightMargin(float margin)
Sets the right margin.

Parameters:
margin - The right margin in points.

rightMargin

public float rightMargin()
Returns the right margin in points.


setTopMargin

public void setTopMargin(float margin)
Sets the top margin.

Parameters:
margin - The top margin in points.

topMargin

public float topMargin()
Returns the top margin in points.


setBottomMargin

public void setBottomMargin(float margin)
Sets the bottom margin.

Parameters:
margin - The bottom margin in points.

bottomMargin

public float bottomMargin()
Returns the bottom margin in points.


setShouldPrintBackgrounds

public void setShouldPrintBackgrounds(boolean printBackgrounds)
Determines wether or not to print backgrounds

Parameters:
printBackgrounds - Enables or disables printing of background elements..

shouldPrintBackgrounds

public boolean shouldPrintBackgrounds()
Returns wether or not backgrounds are printed, true if backgrounds are printed, false otherwise.


appendPageWithHTMLStringAndBaseURL

public void appendPageWithHTMLStringAndBaseURL(java.lang.String htmlString,
                                               java.lang.String baseURL)
Appends a page to the PDF document converting HTML to pdf

Parameters:
htmlString - String containing the actual HTML contents.
baseURL - The baseURL for the document.

appendPageWithURL

public void appendPageWithURL(java.lang.String url)
Appends a page to the PDF document converting HTML to pdf

Parameters:
url - Any valid URL pointing to a document eg "http://www.apple.com" or "file:///Developer/Documentation/WebObjects/webobjects.html".
See Also:
appendPageWithHTMLStringAndBaseURL(String, String)