GithubHelp home page GithubHelp logo

chaosevoker / html-to-pdf Goto Github PK

View Code? Open in Web Editor NEW
50.0 7.0 18.0 9.59 MB

A Node wrapper around a Java HTML to PDF converter to allow users to make HTML templates for automated generation of PDFs.

License: MIT License

JavaScript 57.22% Java 37.03% HTML 5.75%

html-to-pdf's Introduction

html-to-pdf

A Node wrapper around a Java HTML to PDF converter to allow users to make HTML templates for automated generation of PDFs. While often in Node this is achieved through phantomjs, for larger PDF reports each report being a large image can make the file very large very quickly. This aims to fix that problem by using the Java library flyingsaucer to do the heavy lifting and providing a Node wrapper around it for ease of use with web applications.

Installation

To install html-to-pdf simply use NPM:

npm install html-to-pdf

How To Use html-to-pdf

To use html-to-pdf in your Node application, just require it:

var htmlToPdf = require('html-to-pdf');

HTML File Conversion Example

You can use convertHTMLFile to convert HTML files to PDFs. Simply provide a path to a source HTML file and a path to a destination PDF file for conversion.

htmlToPdf.convertHTMLFile('path/to/source.html', 'path/to/destination.pdf',
    function (error, success) {
       if (error) {
            console.log('Oh noes! Errorz!');
            console.log(error);
        } else {
            console.log('Woot! Success!');
            console.log(success);
        }
    }
);

HTML String Conversion Example

You can use convertHTMLString to turn a string of HTML into a PDF file. Simply pass in a string of HTML and a path to a destination PDF file for conversion. This is useful for using other templating languages (like Jade or Mustache) where you can convert the template into HTML and then use this to convert it to a PDF.

var html = ...; //Some HTML String from code above

htmlToPdf.convertHTMLString(html, 'path/to/destination.pdf',
    function (error, success) {
        if (error) {
            console.log('Oh noes! Errorz!');
            console.log(error);
        } else {
            console.log('Woot! Success!');
            console.log(success);
        }
    }
);

Debug Mode

If you want to see the output you can set debug mode to true to see the output of the PDF Renderer (debug is false by default):

htmlToPdf.setDebug(true);

Encoding Settings

If using a particular encoding type is important to getting the output you want, you can set the encoding types for both the input and output:

htmlToPdf.setInputEncoding('UTF-8');
htmlToPdf.setOutputEncoding('UTF-8');

Thanks to bplaa-yai for this feature!

Heads Up

html-to-pdf uses a Java process in the background. That means you will need Java installed to use it. Additionally, the Java process utilizes a library called flyingsaucer:

https://github.com/flyingsaucerproject/flyingsaucer

Flyingsaucer has some contraints on CSS styling and some extra styling features to define PDF parameters. html-to-pdf also uses these features by extension. Other than these two contraints, html-to-pdf is very lightweight.

Also, html-to-pdf creates a temporary file while converting. While it will clean up after itself, this file creation/deletion can trigger things like forever to restart the server. Adding the temp file to the ignore list should prevent this from happening.

Thanks

If you decide to use html-to-pdf, thanks for the support! Let me know if you run into any issues or have any ideas!

License

html-to-pdf uses the MIT license. Feel free to use it anywhere for anything!

html-to-pdf's People

Contributors

bplaa-yai avatar danielkermode avatar devinclark avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

html-to-pdf's Issues

Error: spawn java ENOENT

Im running the Linux Centos Machine as Root user and using this command to create the pdf

pdf.setDebug(true);

pdf.convertHTMLString(object.parent.html, 'test.pdf',
function (error, success) {
if (error) {
console.log('Oh noes! Errorz!');
console.log(error);
} else {
console.log('Woot! Success!');
console.log(success);
}
}
);

"8b050436-3325-45f2-b50d-658f28ae8278.html'" < this file is created .. but the pdf is not
the error is :

Oh noes! Errorz!
{ [Error: spawn java ENOENT]
code: 'ENOENT',
errno: 'ENOENT',
syscall: 'spawn java',
path: 'java',
spawnargs:
[ '-jar',
'/var/www/html/server/node_modules/html-to-pdf/PDFRenderer.jar',
'8b050436-3325-45f2-b50d-658f28ae8278.html',
'test.pdf' ] }

Script is not working

Hi,

I was trying to generate pdf from HTML containing some javascript functions which runs on page load.

But it doesn't seems to be working with those javascript codes.

Please help me out that how can i able to run scripts.

Thanks,
Amit

Failed to generate PDF - and went with indefinite loop - no response

I'm trying to generate 5 page PDF with dynamic data, it works only when I enable DEBUG i.e

htmlToPdf.setDebug(true);

Also in console it show debug log as below

STDERR: line 1,376 column 115 - Warning: trimming empty <div>

STDERR: line 1,379 column 109 - Warning: trimming empty <span>

STDERR: line 1,379 column 116 - Warning: trimming empty <div>

STDERR: line 1,382 column 109 - Warning: trimming empty <span>

STDERR: line 1,382 column 116 - Warning: trimming empty <div>

STDERR: InputStream: Doctype given is ""

STDERR: InputStream: Document content looks like HTML 4.01

STDERR: 666 warnings, no errors were found!

Any suggestion to get it working without DEBUG mode ON?

How to provide doctype

I get this error using convertHTMLString method:

STDERR: InputStream: Doctype given is ""

STDERR: InputStream: Document content looks like HTML 4.01

my html string:

var html =
<!DOCTYPE html>

Error "spawn java ENOENT" when convert html file to pdf

Dear @ChaosEvoker .
My OS is Win7 64, is possible here to run your module?
Because I found the path syntax is confilct:
[Error: spawn java ENOENT]
code: 'ENOENT',
errno: 'ENOENT',
syscall: 'spawn java',
path: 'java',
spawnargs:
[ '-jar',
'C:\Projects\Demo\node_modules\html-to-pdf/PDFRenderer.jar',
'C:\Projects\Demo\label.html',
'/temp/destination.pdf' ] }

corrupted pdf for large HTML files

It shows success but the pdf is corrupted.

Any help?

ERROR:

STDERR: ERROR: 'Premature end of file.'
STDERR:

STDERR: Exception in thread "main"
STDERR: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:58)
Caused by: org.xhtmlrenderer.util.XRRuntimeException: Can't load the XML resource (using TRaX transformer). org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; Premature end of file.
at org.xhtmlrenderer.resource.XMLResource$XMLResourceBuilder.createXMLResource(XMLResource.java:191)
at org.xhtmlrenderer.resource.XMLResource.load(XMLResource.java:71)
at org.xhtmlrenderer.swing.NaiveUserAgent.getXMLResource(NaiveUserAgent.java:211)
at org.xhtmlrenderer.pdf.ITextRenderer.loadDocument(ITextRenderer.java:134)
at org.xhtmlrenderer.pdf.ITextRenderer.setDocument(ITextRenderer.java:138)
at PDFRenderer.main(PDFRenderer.java:84)
... 5 more
Caused by: javax.xml.transform.TransformerException: org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; Premature end of file.
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:749)
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:351)
at org.xhtmlrenderer.resource.XMLResource$XMLResourceBuilder.createXMLResource(XMLResource.java:189)
... 10 more
Caused by: org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; Premature end of file.
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1239)
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transformIdentity(TransformerImpl.java:668)
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:737)
... 12 more

Woot! Success!
HEREE --- Added order Images -------

pdf file not created

I use html-to-pdf module it give me sucess but when i check pdf file is not created.when i Debug it it give me following errors:

STDERR: Exception in thread "main"
STDERR: java.lang.reflect.InvocationTargetException
STDERR:

STDERR: at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
STDERR:

STDERR: at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
STDERR:

STDERR: at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
STDERR:

STDERR: at java.lang.reflect.Method.invoke(Unknown Source)
STDERR:

STDERR: at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:58)
STDERR:

STDERR: Caused by: java.lang.Error: factory already defined
STDERR:

STDERR: at java.net.URL.setURLStreamHandlerFactory(Unknown Source)
STDERR:

STDERR: at PDFRenderer.main(PDFRenderer.java:32)

STDERR: ... 5 more
STDERR:

please help me ?

Change background color of pdf

Hi,

How I can change the background color? Please help. I have the background to my pdf but still I can see the white color after border in pdf. How I can change that.
fdsf

UTF-8 doesnt work

I set both
htmlToPdf.setInputEncoding('UTF-8'); htmlToPdf.setOutputEncoding('UTF-8');
as utf-8 but it doesnt work at all it just omit the different language text part in pdf result file and doesnt show it

TOC (Table of Contents)

Is it possible? To put links using <a href="#my-anchor"> My anchor </a> to link to a piece of text in another page?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.