Git Product home page Git Product logo

dynamic-html-pdf's Introduction

How to Setup: Dynamic handlebars html pdf to create dynamic content html to pdf.

Installation

npm install dynamic-html-pdf --save

Create template.html

<html>
    <head>
        Dynamic HTML to PDF
    </head>
    <body>
        <h1>Hi {{users.0.name}}</h1>
        <div>
            template
            {{#ifCond 'v1' 'v2'}}
                {{v1}} is equals to {{v2}}
            {{else}}
                Variables are not similar
            {{/ifCond}}
        </div>
    </body>
</html>

Feel free to use handlebar syntax: Handlebar builtin helpers

For example:

<ul>
  {{#each users}}
    <li>Name: {{this.name}}</li>
    <li>Age: {{this.age}}</li>
    <li>DOB: {{this.dob}}</li>
  {{/each}}
</ul>

For example: Register helper inside js file:

// Custom If condition inside handlebar(JS file)
var pdf = require('dynamic-html-pdf');
pdf.registerHelper('ifCond', function (v1, v2, options) {
    if (v1 === v2) {
        return options.fn(this);
    }
    return options.inverse(this);
})

Utilize registered helper inside handlebar template:

{{#ifCond v1 v2}}
    {{v1}} is equals to {{v2}}
{{else}}
    Variables are not similar
{{/ifCond}}

How to use Dynamic HTML to PDF

var fs = require('fs');
var pdf = require('dynamic-html-pdf');
var html = fs.readFileSync('template.html', 'utf8');

// Custom handlebar helper
pdf.registerHelper('ifCond', function (v1, v2, options) {
    if (v1 === v2) {
        return options.fn(this);
    }
    return options.inverse(this);
})

var options = {
    format: "A3",
    orientation: "portrait",
    border: "10mm"
};

var users = [
    {
        name: 'aaa',
        age: 24,
        dob: '1/1/1991'
    },
    {
        name: 'bbb',
        age: 25,
        dob: '1/1/1995'
    },
    {
        name: 'ccc',
        age: 24,
        dob: '1/1/1994'
    }
];

var document = {
    type: 'buffer',     // 'file' or 'buffer'
    template: html,
    context: {
        users: users
    },
    path: "./output.pdf"    // it is not required if type is buffer
};

pdf.create(document, options)
    .then(res => {
        console.log(res)
    })
    .catch(error => {
        console.error(error)
    });

dynamic-html-pdf's People

Contributors

dependabot[bot] avatar erikkallen avatar navjotdhanawat 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

Watchers

 avatar  avatar  avatar

dynamic-html-pdf's Issues

Image url in CSS

Hello, i need to put a background image on each page of my template so i'm doing :

<div class="page" style="background-image: url('{{url}}/mypath/background.jpg'); background-size: cover;">

{{url}} is the url of my website.
And it's not working but same path is working for the image :
<img src="{{url}}/mypath/img.jpg">

Do you know how i can fix it ?

(I already try relative path or adding !important)

how to repeat table header/footer?

I am trying to render a table in pdf which spawns across multiple pages. I want the header to appear on every page.
I tried multiple things including having a thead, tbody, thead {display: table-header-group; } etc but nothing worked.
When viewed in html and printed to pdf from the browser, the page renders correctly with table headers on each page. Please help.

write EPIPE error in aws lambda

ERROR { Error: write EPIPE
at afterWriteDispatched (internal/stream_base_commons.js:78:25)
at writeGeneric (internal/stream_base_commons.js:73:3)
at Socket._writeGeneric (net.js:714:5)
at Socket._write (net.js:726:8)
at doWrite (_stream_writable.js:415:12)
at writeOrBuffer (_stream_writable.js:399:5)
at Socket.Writable.write (_stream_writable.js:299:11)
at PDF.PdfExec [as exec] (/var/task/node_modules/html-pdf/lib/pdf.js:141:15)
at PDF.PdfToBuffer [as toBuffer] (/var/task/node_modules/html-pdf/lib/pdf.js:44:8)
at Promise (/var/task/node_modules/dynamic-html-pdf/index.js:40:24)
at new Promise ()
at Object.module.exports.create (/var/task/node_modules/dynamic-html-pdf/index.js:25:12)
at Promise (/var/task/routes/ar_invoice.js:795:17)
at new Promise ()
at exportHtmlToPdf (/var/task/routes/ar_invoice.js:763:16)
at ar_checklist_details.findAll.then (/var/task/routes/ar_invoice.js:379:80)
at tryCatcher (/var/task/node_modules/bluebird/js/release/util.js:16:23)
at Promise._settlePromiseFromHandler (/var/task/node_modules/bluebird/js/release/promise.js:512:31)
at Promise._settlePromise (/var/task/node_modules/bluebird/js/release/promise.js:569:18)
at Promise._settlePromise0 (/var/task/node_modules/bluebird/js/release/promise.js:614:10)
at Promise._settlePromises (/var/task/node_modules/bluebird/js/release/promise.js:694:18)
at _drainQueueStep (/var/task/node_modules/bluebird/js/release/async.js:138:12) errno: 'EPIPE', code: 'EPIPE', syscall: 'write' }

half of the page gets cut

The handlebars page renders fine in browser, but when creating pdf using this lib, the page is cut in half horizontally. So, around 40% of the right side of the page is not visible in pdf

EDIT:
the same page(originally A4) is less cut in A3 size, and almost perfect in A2 size

Error: write EPIPE

When I tested in localhost, it works 100%. But when I deploy my project to server, I got an error. I already install all requirement in Dockerfile, but I still got the error.

npm install fs --save && npm install dynamic-html-pdf --save

Below here, the error I got
Error: write EPIPE at afterWriteDispatched (internal/stream_base_commons.js:78:25) at writeGeneric (internal/stream_base_commons.js:73:3) at Socket._writeGeneric (net.js:714:5) at Socket._write (net.js:726:8) at doWrite (_stream_writable.js:415:12) at writeOrBuffer (_stream_writable.js:399:5) at Socket.Writable.write (_stream_writable.js:299:11) at PDF.PdfExec [as exec] (/app/node_modules/html-pdf/lib/pdf.js:141:15) at PDF.PdfToBuffer [as toBuffer] (/app/node_modules/html-pdf/lib/pdf.js:44:8) at Promise (/app/node_modules/dynamic-html-pdf/index.js:40:24) at new Promise (<anonymous>) at Object.module.exports.create (/app/node_modules/dynamic-html-pdf/index.js:25:12) at /app/app/controllers/bookingController.js:939:23 at /app/node_modules/mongoose/lib/model.js:4889:16 at /app/node_modules/mongoose/lib/helpers/promiseOrCallback.js:24:16 at /app/node_modules/mongoose/lib/model.js:4912:21 errno: 'EPIPE', code: 'EPIPE', syscall: 'write' }"

Grey square in place of image

Hello,
Plugin has always worked in last 1 year and never got a problem producing PDF with images with <img> tag.
But today we found that new pdf's are not displaying header logo anymore. See attached file on top-left you will see a grey little square instead of image that is a classic PNG you can check/view/download at https://b2b.o-jersey.com/uploads/file-1633732451913.png
Everything is still working pretty well in development (windows) but for some reason we cannot understand, it stopped working on linux. We tried with JPG from same domain/path and problem remains. We tried with another random image from another domain and it works! This strange unexpected behaviour seems to be bound to server (dynamic-html-pdf 1.0.4 + Ubuntu 18.04 LTS + nginx 1.14.0 + nodejs 13.13). Can you check and help us to solve this issue? Let me know if you need more info/details.
Thank you in advance.
order#3.pdf

UPDATE:

We also tried to produce a PDF with same image (https://b2b.o-jersey.com/uploads/file-1633732451913.png) from windows dev environment and it works perfectly.

WORKAROUND:

We successfully made an experiment by putting inside <img> tag a simple ENDPOINT of another server. Here below php code snippet:

$url = $_GET["image"];
$headers = get_headers( $url );
$type = $headers[3];
$size = $headers[4];
$imageString = file_get_contents($url);
header('Content-Type: '.$type);
header('Content-length: '.$size);
echo $imageString;
exit();

Basically it does nothing but read the remote image (see image parameter) and "spit it out" as its own. So we can use it as <img src="https://www.../path/to/endpoint.php?image=https://b2b.o-jersey.com/uploads/file-1633732451913.png"> and it works (see attached order#3 (FIXED).pdf). Don't know if it can help anyone but that's how we could solve this issue...still wondering why it fails to display when src is set to original image url.
order#3 (FIXED).pdf

stream pdf

hey men i really need help i want to display it first in the browser like this:

3

Not able to identify file has been successfully put in location.

Is that any way to identify the pdf file has been now generated?
Example of my case, the code is one server and pdf path is another server, the size and data are large. I am getting a response pdf path. but when I hit the pdf path in the browser show me 404 file is not found. retry after the 10-sec file is open now on the same location.

Flexbox in HTML template not working

Hello,
I've just started to use this npm package and it looks really simple and powerful. However it seems that using flexbox in the template.html file is not working and the output PDf is not styled with those propreties.
Is this expected behaviour or is this a configuration probleme ?

Thanks !

Links

hello,

Is it possible to display links correctly.
Now i'm getting this:
image
The link is clickable but i want to remove the web address and make the link look blue and underlined.

I rendered my html variables with {{{variable}}} but the links contents doesnt display properly.
Any help on this?

Render a variable as html, not as plaintext

I have a variable that needs to be rendered in the template. The variable contains HTML. In the pdf the html is rendered as plain text. I need the html variable to be rendered as html. Is this possible with your template engine?

Linux body font-size

Hello,
I'm trying to figure out why I got different results creating PDF on same software configuration but different OS. More details:
Config 1
Nodejs 13.13.0
[email protected]
Windows 10 x64
===> GOT result1.pdf
result1.pdf

Config 2
Nodejs 13.13.0
[email protected]
Linux Ubuntu 18.04 LTS
===> GOT result2.pdf
result2.pdf

Any help would be highly appreciated.
Best Regards
Angelo

Block helpers are not working

I have used some block helpers in my handlebar files. When I convert it to Pdf, then those block helpers conditions are not worked.

For Ex; I have used {{#ifCond title '==' 'Dashboard' }} active {{/ifCond}}".
It worked in my original file but not in pdf

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.