css2pdf is a simple to use Javascript for CSS to PDF rendering that can easily be plugged-in to your website.
Please visit the complete demonstration website at http://www.cloudformatter.com/css2pdf
The basic method is to include a print button in your website with a click handler that calls the xepOnline.Formatter method "Format". The "Format" method takes a single element by ID or an array of element IDs. The resulting PDF is a single set of pages for each element ID (and children). PDF is produced from a hosted PDF rendering service. PDF is returned to the client browser as either an embeddable PDF or as a download. Certain features (like embedding PDFs) are not available on all browsers. Safari and Internet Explorer only support download. Chrome and Firefox support both download or embed.
All HTML, CSS, and CSS media styling will be applied to the print selection(s) including print media CSS rules. Note: some framework's do have undesirable print media rules, therefore we have chosen to eliminate these by default.
Be sure to include the jQuery Javascript library.
You can either just include the one from this website or download.
<script src="https://github.com/Xportability/css-to-pdf/raw/master/xepOnline.jqPlugin.js"></script>
You can either change the external references in the Javascript from http: to https: or override their values in your own Javascript. You would change (or override) to these values:
xep_chandra_service: 'https://xep.cloudformatter.com/Chandra.svc/genpackage',
xep_chandra_service_AS_PDF: 'https://xep.cloudformatter.com/Chandra.svc/genfile',
xep_chandra_service_page_images: 'https://xep.cloudformatter.com/Chandra.svc/genpageimages',
Then you include a print button element like this in your HTML:
<a href="#" onclick="return xepOnline.Formatter.Format('Usage');">
<img src="https://github.com/Xportability/css-to-pdf/raw/master/button-print.png">
</a>
Where "Usage" is the ID of the element to print.
A number of options can be passed to achieve various effects on the print output, including page media sizing and margins, as well as instruct the rendering engine to return an embedded PDF in a new window or a downloadable PDF. All the options are described below.
<a href="#" onclick="return xepOnline.Formatter.Format('Usage',
{pageWidth:'216mm', pageHeight:'279mm'});">
<img src="https://github.com/Xportability/css-to-pdf/raw/master/button-print.png">
</a>
<a href="#" onclick="return xepOnline.Formatter.Format('Usage',{render:'download'});">
<img src="https://github.com/Xportability/css-to-pdf/raw/master/button-print.png">
</a>
<a href="#" onclick="return xepOnline.Formatter.Format('testDIV',{render:'download',
cssStyle:[{fontSize:'30px'},{fontWeight:'bold'}]});">
<img src="https://github.com/Xportability/css-to-pdf/raw/master/button-print.png"/>
</a>
<a href="#" onclick="return xepOnline.Formatter.Format('testDIV',{render:'download',
namespaces:['xmlns:ng="http://www.foo.net"']});">
<img src="https://github.com/Xportability/css-to-pdf/raw/master/button-print.png"/>
</a>
All options are optional and will defer to their default values when not specified
This plugin computes the CSS style for each element within the selected print container(s), including all internal, external, and print media CSS rules puts the style directly on the element. The "computed" html source is then sent to the @cloudformatter XEPOnline rendering engine. @cloudformatter is capable of receiving any XML document with an embedded XSL Stylesheet reference for formatting. The rendering engine translates the "computed" XHTML3 source to XSL-FO4 and then Renders the PDF.
@cloudformatter rendering service is configured with many fonts including the 50 most popular Google Fonts . To try out these fonts visit our demo page. Feel free to use any of these fonts your web applications and they will be used perfectly in your PDF rendering! Refer to Google Fonts for more information on this Open Source service.