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

@paralect/pdf-service-client

v0.5.1

Published

Pdf service client for https://hub.docker.com/r/paralect/pdf-service/

Downloads

200

Readme

Pdf service client side

This is the client side of PDF service description. The main aim of this part is to build html file with inline css, font and images. To use it you should start the server first.

Index

  1. Overview
  2. Installation
  3. Quick example
  4. API
    1. generatePdf(pdfPath, [options])
    2. generatePdfByContent(html, [options])
    3. Pdf options
    4. generateImage(imgPath, [options])
    5. generateImageByContent(html, [options])
    6. Image options
  5. Debugging
  6. Examples

Overview

This is the client library that provides easy pdf and image generation. Here some features that can help you to develop awesome things:

  1. :bicyclist: Handlebars under the hood. You create your images and pdfs with dynamical content.
  2. :package: Webpack will manage all your assets. You just need to save assets near to your html template.
  3. :question: Maybe is it your feature? You can request new feature in issues or add new pr

Init

You should install @paralect/pdf-service-client:

npm i @paralect/pdf-service-client

Quick example

In your js file write these lines (be sure that you started server):

const PdfService = require('@paralect/pdf-service-client'); // require client pdf service library
const fs = require('fs'); // fs to write file

// pdf service init
const pdfService = new PdfService({
  serverUrl: 'http://localhost:3000',
  mode: 'development',
});

// generate pdf by html string
pdfService.generatePdfByContent('<body><h1>Hello, {{name}}!</h1></body>', {
  pdfOptions: {
    format: 'Letter',
  },
  templateSystem: {
    params: {
      name: 'Your name',
    },
  },
}).then((pdfStream) => {
  const writeStream = fs.createWriteStream('./hello.pdf');

  pdfStream.pipe(writeStream);

  writeStream.on('finish', () => {
    console.log('Hello pdf was created!');
  });
});

Execution of this code should generate pdf file with 'Hello, Your name' string.

A bit of constructor explanation:

const pdfService = new PdfService({
  serverUrl: 'http://localhost:3000', // optional
  mode: 'development', // optional
});
  1. serverUrl - you can provide url to server (look options sections here). http://localhost:3000 will be used as default.
  2. mode - mode can be production or development. In production mode you have to build assets before you invoke generatePdf method.

API

When you initialise pdf service it will provide you several methods.

generatePdf(pdfPath, [options])

Let's describe these options:

  1. pagePath - it is the path to html file which will be transformed to pdf.
  2. options - it is optional param. You can see more in Pdf options section.

This method returns stream with your pdf file.

Note: You page that was specified by pagePath should be placed with all assets in one directory. This directory should be isolated from other codebase. You have this restriction because build of all assets looks for all files that was placed in the same directory with your html source. Look to Example for more details.

generatePdfByContent(html, [options])

Let's describe these options:

  1. html - it is the html text which will be transformed to pdf.
  2. options - it is optional param. You can see more in Pdf options section.

This method returns stream with your pdf file.

Pdf options

Here is the example of image options:

  {
    pdfOptions: { // optional
      format: 'Letter',
    },
    headers: { // optional
      Authorization: 'Bearer ...'
    },
   templateSystem: { // optional
      params: {
      tasks: [{}],
      },
      helpers: {
        hours: Handlebars => (hours) => {
          const htmlData = parseFloat(Handlebars.escapeExpression(hours)).toFixed(2);

          return new Handlebars.SafeString(htmlData);
        },
      },
      partials: {
        hello: '<h1> Hello! </h1>',
      },
    },
  }

Let's describe these options: 2. pdfOptions - you can provide pdf options (look options sections here). 3. headers - you can provide headers which will be used on the page, for example you can add authorization header (look here) 4. templateSystem - if you are using Handlebars template then you can provide properties which is used on template (params, helpers, partials).

generateImage(imgPath, [options])

Let's describe these options:

  1. pagePath - it is the path to html file which will be transformed to pdf.
  2. options - it is optional param. You can see more in Image options section.

This method returns stream with your img file.

Note: You page that was specified by pagePath should be placed with all assets in one directory. This directory should be isolated from other codebase. You have this restriction because build of all assets looks for all files that was placed in the same directory with your html source. Look to Example for more details.

generateImageByContent(html, [options])

Let's describe these options:

  1. html - it is the html text which will be transformed to pdf.
  2. options - it is optional param. You can see more in Image options section.

Image options

Here is the example of pdf options:

  {
    imgOptions: { // optional
      format: 'Letter',
    },
    headers: { // optional
      Authorization: 'Bearer ...'
    },
   templateSystem: { // optional
      params: {
      tasks: [{}],
      },
      helpers: {
        hours: Handlebars => (hours) => {
          const htmlData = parseFloat(Handlebars.escapeExpression(hours)).toFixed(2);

          return new Handlebars.SafeString(htmlData);
        },
      },
      partials: {
        hello: '<h1> Hello! </h1>',
      },
    },
  }

Let's describe these options: 2. imgOptions - you can provide image options (look options sections here). 3. headers - you can provide headers which will be used on the page, for example you can add authorization header (look here) 4. templateSystem - if you are using Handlebars template then you can provide properties which is used on template (params, helpers, partials).

Debugging

To start library in debug mode you just need to start it with DEBUG=pdf-service. This will provide additional logs.

Examples

You can find the samples in here. To run the sample just write in samples directory:

 docker-compose up -d
 node index.js

The first command should start pdf server which listen on 4444 port. You can specify another port in docker-compose.yml file. All works in this way:

That's all folks!

Change Log

This project adheres to Semantic Versioning. Every release is documented on the Github Releases page.

License

Ship is released under the MIT License.

Contributing

Please read CONTRIBUTING.md for details on our code of conduct, and the process for submitting pull requests to us.

Contributors

Thanks goes to these wonderful people (emoji key):

| KuhArt💻 📖 🐛 | Uladzimir Mitskevich🤔 🐛 | NesterenkoNikita🤔 🐛 | Andrew Orsich🤔 🐛 🎨 | Evgeny Zhivitsa💻 🎨 | Женя Филиппович🐛 | | :---: | :---: | :---: | :---: | :---: | :---: |

This project follows the all-contributors specification. Contributions of any kind welcome!