Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate a pdf from html web page? [closed]

I'm looking for a library to transform my web page into a PDF file after click event from a button. I'm trying jspdf, but it prints without the CSS, how can I make this using JavaScript/jQuery and keep my CSS? Or another CSS that I can choose?

like image 229
Alejandro Hinestrosa Avatar asked Jul 30 '14 20:07

Alejandro Hinestrosa


People also ask

How do I create a closed PDF?

Check Restrict editing and printing of the document box. Enter a password you would like to use to protect the file, and select permissions for Printing and Changes, and click OK. You'll have to re-enter the password before the window closes. Save the file and close it.


2 Answers

There is a new jQuery + cloud solution that will render any HTML page and its CSS (including print media rules) to PDF. The solution is setup to print any region of your webpage, you just tell the Formatter which container element you want to print and the library does the rest. What you get back is an embeddable PDF or the backend will push back a PDF for download.

Here's your library (GitHub):
https://github.com/Xportability/css-to-pdf

Here's your fiddle:
http://jsfiddle.net/kstubs/jrtM5/

Here's a working demo:
http://xep.cloudformatter.com/doc/

Currently there are not useage instructions, but following along with the samples (view source) should be pretty self-explanatory (look for the Print It buttons) and here is, more or less the additional options/parameters that the Format method understands.

options 
{
    pageWidth: "8.5in",             
    pageHeight: "11in", 
    pageMargin: ".25in", 
    pageMarginTop: "1in",
    pageMarginRight: "1in",
    pageMarginBottom: "1in",
    pageMarginLeft: "1in",
    render: ("none"|"newwin<default>"|embed"|"download<default IE>"),
    foStyle: { 
        // puts fo style attributes on the root, ex. fontSize:14px
        foStyleName: 'value', ...
    }           
}

Here is list of CSS attributes that are currently supported.

[
    'lineHeight', 
    'alignmentBaseline', 
    'backgroundImage', 'backgroundPosition', 'backgroundRepeat', 'backgroundColor',
    'baselineShift', 
    'border',
    'borderWidth', 'borderColor','borderStyle',
    'borderTop','borderLeft','borderRight','borderBotttom',
    'borderTopWidth','borderTopStyle','borderTopColor', 
    'borderBottomWidth','borderBottomStyle','borderBottomColor',
    'borderLeftWidth','borderLeftStyle','borderLeftColor',
    'borderRightWidth','borderRightStyle','borderRightColor',
    'borderCollapse',             
    'clear', 'color', 
    'display', 'direction', 'dominantBaseline', 
    'fill', 'float', 
    'fontStyle', 'fontVariant', 'fontWeight', 'fontSize', 'fontFamily', 
    'listStyleType', 'listStyleImage', 'letterSpacing', 
    'marginTop', 'marginBottom', 'marginLeft', 'marginRight','orphans', 
    'paddingTop', 'paddingRight', 'paddingBottom', 'paddingLeft',
    'pageBreakAfter', 'pageBreakBefore', 
    'tableLayout', 
    'textAlign', 'textAnchor','textDecoration', 'textIndent', 'textTransform', 
    'verticalAlign',
    'widows', 'wordSpacing', 'width'
]

Hope this helps!

like image 183
kstubs Avatar answered Oct 21 '22 15:10

kstubs


try this npm package htmlto.It creates PDF from html with CSS styling

var htmlTo = require('htmlto')

htmlTo.pdf('./public/html/report.html','./public/pdf/report.pdf',{width: 2480, height: 3508})

install:

npm install -S htmlto

npm install -S phantom

*you can also specify the dimensions.phantom version ^4.0.3 and node version v6.5.0 https://www.npmjs.com/package/htmlto

like image 24
dev07 Avatar answered Oct 21 '22 16:10

dev07