npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@sagar.modhvaniya/html2pdf

v1.0.3

Published

Convert HTML string into pdf

Downloads

15

Readme

html2pdf for NodeJS

Simple and lightweight HTML to text conversion using NodeJs.

  • highly customize interactive PDF creation with dynamic design with dynamic JSON.
  • can convert any string (HTML or simple) pdf.

Installation

npm install @sagar.modhvaniya/html2pdf

Conversion API

The API exposes a single function 'convertPdf'. Using this function, you can input a multitude of settings, which are further specified below:

var pdf = require('@sagar.modhvaniya/html2pdf');

pdf.convertPdf(options).then((pdfPath)=>{
	/* return new created pdf path */

}).catch((error)=>{
    console.error("Error in create Pdf");
})

Options

Calling convert() requires an options object, which includes the following definitions:

{
	"htmlString" : "HTML string for pdf with liquidJs string if need",
	"externalCss" : "string to additional CSS file",
	"inputJson" : "JSON data to make dynamic string",
	"filePath" : "Path to save file",
	"fileName" : "Pdf file name",
	"displayHeaderFooter" :" true or false If you want to add header or footer in pdf",
        "headerTemplate": "HTML string for header",
        "footerTemplate": "HTML string for footer"
}

Code example

  • JSON data which I need to show in table
{
    "employees": [
        {
            "firstName": "John",
            "lastName": "Smith",
            "email": "[email protected]",
            "due": "50",
            "website": "http://www.jsmith.com"
        },
        {
            "firstName": "Frank",
            "lastName": "Bach",
            "email": "[email protected]",
            "due": "50",
            "website": "http://www.frank.com"
        },
        {
            "firstName": "Jason",
            "lastName": "Doe",
            "email": "[email protected]",
            "due": "100",
            "website": "http://www.jdoe.com"
        },
        {
            "firstName": "Tim",
            "lastName": "Conway",
            "email": "[email protected]",
            "due": "50",
            "website": "http://www.timconway.com"
        }
    ]
}
  • HTML for display data into table with template language
const htmlString = `<html>
                    <head>
                        <style>
                            body {
                                font: normal medium/1.4 sans-serif;
                            }
                    
                            table {
                                border-collapse: collapse;
                                width: 100%;
                            }
                    
                            th, td {
                                padding: 0.25rem;
                                text-align: left;
                                border: 1px solid #ccc;
                            }
                    
                            tbody tr:nth-child(odd) {
                                background: #eee;
                            }
                        </style>
                    </head>
                    <body>
                    <table class="zebra">
                        <thead>
                        <tr>
                            <th>Last Name</th>
                            <th>First Name</th>
                            <th>Email</th>
                            <th>Due</th>
                            <th>WebSite</th>
                        </tr>
                        </thead>
                        <tbody>
                        {% for employee in employees %}
                        <tr>
                            <td> {{ employee.lastName }}</td>
                            <td> {{ employee.firstName }}</td>
                            <td> {{ employee.email }}</td>
                            <td> {{ employee.due }}</td>
                            <td> {{ employee.website }}</td>
                        </tr>
                        {% endfor %}
                        </tbody>
                    </table>
                    </body>
                    </html>
`
  • Final code
var pdf = require('@sagar.modhvaniya/html2pdf');
const companyData = {
    "employees": [
        {
            "firstName": "John",
            "lastName": "Smith",
            "email": "[email protected]",
            "due": "50",
            "website": "http://www.jsmith.com"
        },
        {
            "firstName": "Frank",
            "lastName": "Bach",
            "email": "[email protected]",
            "due": "50",
            "website": "http://www.frank.com"
        },
        {
            "firstName": "Jason",
            "lastName": "Doe",
            email: "[email protected]",
            "due": "100",
            "website": "http://www.jdoe.com"
        },
        {
            "firstName": "Tim",
            "lastName": "Conway",
            "email": "[email protected]",
            "due": "50",
            "website": "http://www.timconway.com"
        }
    ]
}

var options = { 
    htmlString,
    inputJson:companyData,
    filePath:'./',
    fileName:'employee_report.pdf',
    externalCss:null,
    displayHeaderFooter:false,
    headerTemplate:null,
    footerTemplate:null
 };

pdf.convertPdf(options).then((pdfPath)=>{
	/* return new created pdf path */
    console.log(pdfPath)
}).catch((error)=>{
    console.error("Error in create Pdf");
})

Reference

License

MIT