Example for adding a PDF generator to your umbraco site
Some time ago I created a solution for generating pdf documents from your umbraco content (and from the outputted html). I always wanted to make a package out of this but since the project relies on the commercial pdf generating library ABCpdf this isn’t possible. The main reason for using ABCpdf is that it’s html to pdf options are pretty decent.
So instead of releasing a package I’ll share the sourcecode and I’ll give an overview of how to use it.
Download: PDFGen sourcecode
Installation
Once you have ABCpdf installed (it’s possible to download a trail and you can also apply for a free license) the installation is just a mather of dropping some files in your umbraco instance.
- Drop PDFGen.dll in the /bin folder
- Drop PDFGen.aspx in the /umbraco
- Drop PDFGen.config in the /config folder
Setup
First you’ll need to define a pdf template, this can be done in the PDFGen.config file which should
be in the /config folder.
Lets look at an example config file:
<?xml version=”1.0″ encoding=”utf-8″ ?>
<PDFGen PermitPrint=”False” PermitExtractContent=”False”>
<!– Header–>
<Text FontName=”Arial” FontSize=”12″ FontColor=”0,0,0″ X1=”20″ Y1=”40″ X2=”500″ Y2=”700″
IsRecurring=”true”>my header text</Text>
<Image File=”/media/950/joblistpdfhead.jpg” X=”20″ Y=”670″ Width=”570″ Height=”100″ IsRecurring=”true”/>
<!– Content –>
<Property RequestParam=”test” PropertyAlias=”title;@createDate” FontName=”Arial” FontSize=”18″
FontColor=”0,0,0″ X1=”25″ Y1=”620″ X2=”500″ Y2=”650″ IsRecurring=”false”>{0} - {1}</Property>
<Property RequestParam=”" PropertyAlias=”info” FontName=”Arial” FontSize=”11″ FontColor=”0,0,0″ X1=”25″
Y1=”180″ X2=”530″ Y2=”630″ IsRecurring=”false”></Property>
<Property RequestParam=”" PropertyAlias=”linkURL” FontName=”Arial” FontSize=”11″ FontColor=”0,0,0″
X1=”40″ Y1=”715″ X2=”520″ Y2=”740″ IsRecurring=”false”></Property>
<!– Footer –>
<Text FontName=”Arial” FontSize=”6″ FontColor=”0,0,0″ X1=”25″ Y1=”20″ X2=”500″ Y2=”30″
IsRecurring=”true”>Copyright mysite 2009 — All Rights Reserved.</Text>
</PDFGen>
After the xml declaration (yes the config file is an xml file) you have the PDFGen root node.
On this node you can set wether the generated pdf files are printable and if it is possible to extract
content (copy,paste).
<PDFGen PermitPrint=”False” PermitExtractContent=”False”>
The child nodes of the root PDFGen node will be used to define the pdf. There are a couple of
possibilities:
- Text
- Image
- Property
- RenderedOutput
Text nodes
<Text FontName=”Arial” FontSize=”12″ FontColor=”0,0,0″ X1=”20″ Y1=”40″ X2=”500″ Y2=”700″
IsRecurring=”true”>my header text</Text>
A text node is used to add some static text on the pdf document it has several attributes
- FontName: name of the font family that will be used
- FontSize: size of the font
- FontColor: color of the font
- X1,Y1,X2,Y2: these are the coordinated used to define a box where the text will be placed
- IsRecurring: if this is set to true the text will appear on each page (used for headers and
footers) - The inner text is the static text that will be placed on the pdf (it is possible to add html).
Image nodes
<Image File=”/media/950/joblistpdfhead.jpg” X=”20″ Y=”670″ Width=”570″ Height=”100″ IsRecurring=”true”/>
An image node is used to add an image to the pdf document.
An overview of it’s attributes:
- File: path to the image
- X,Y: coordinated of the image
- Width: width of the image
- Height: height of the image
- IsRecurring: if this is set to true the image will appear on each page (used for headers and
footers)
Property nodes
<Property RequestParam=”test” PropertyAlias=”title;@createDate” FontName=”Arial” FontSize=”18″
FontColor=”0,0,0″ X1=”25″ Y1=”620″ X2=”500″ Y2=”650″ IsRecurring=”false”>{0} – {1}</Property>
A property node is used to add umbraco content node property data to the pdf document.
An overview of it’s attributes:
- RequestParam: if this is empty the default request param id will be used, if you want to use
another one define it here - PropertyAlias: alias of the property (possible to add multiple separated by semicolon)
- FontName: name of the font family that will be used
- FontSize: size of the font
- FontColor: color of the font
- X1,Y1,X2,Y2 :these are the coordinated used to define a box where the text will be placed
- IsRecurring :if this is set to true the text will appear on each page (used for headers and
footers)
The innertext can be used to add some static text around the property or if you have multiple
propertyaliases you must define how they will be placed (simular to string.format).
RenderedOutput nodes
<RenderedOutput RequestParam=”" Template=”" X1=”25″ Y1=”620″ X2=”500″ Y2=”650″ IsRecurring=”false” />
This will fetch the rendered output of a page, if no requestparam is used it will use the id one and if no template alias is defined it will use that default template of that page.
An overview of it’s attributes:
- RequestParam: if this is empty the default request param id will be used, if you want to use
another one define it here - Template: template alias to use, if this is empty the default template of the document will be used
- X1,Y1,X2,Y2 :these are the coordinated used to define a box where the text will be placed
- IsRecurring :if this is set to true the text will appear on each page (used for headers and
footers)
Usage
Once installed and if you have a working config file. You can use pdfgen by calling /umbraco/pdfgen.aspx?id=1234 (the id is the id of the umbraco document).
If you have defined other RequestParams in your config file (like headerid) you just need to do:
/umbraco/pdfgen.aspx?id=1234&headerid=2345
It’s also possible to have multiple config files (just copy the pdfgen.config) if you want to use anohter config file you can do it like this:
/umbraco/pdfgen.aspx?id=1234&pdfconfig=pdfconfigcopy.config
pdfconfigcopy.config is the filename of the configfile (must be in the /config dir)
