pdfkiwi

0.1.1 • Public • Published

pdfkiwi-node

A node library for interacting with pdf.kiwi API

Build Status Coverage Status

Installation

# - Npm 
npm install pdfkiwi --save
 
# - Yarn 
yarn add pdfkiwi

Usage

Start by instantiating a client instance:

const pdf = require('pdfkiwi');
 
const client = new pdf.Pdfkiwi('[api email]', ['api token']);

You can then use any of the methods described below.

  • Available options (for the options parameters) are listed on this page.
  • API error codes are listed on this page.
  • All of these methods return a promise.
  • The promise is resolved with a Buffer containing the PDF binary data,
    You can use built-in utility functions to save or download the pdf directly.
    (see Built-in utility functions)

.convertHtml() - Convert HTML string to PDF

Pdfkiwi.convertHtml(html: String|Number, options: Object): Promise

Exemple:

client.convertHtml('<h1>Hello world</h1>', { orientation: 'landscape' })
    .then(pdf.saveToFile('./my-pdf-file.pdf'))
    .then(() => { console.log('done !'); })
    .catch((err) => {
        // err.code   : Api error code (if available)
        // err.status : Http code (if available)
        console.log(err);
    });

Built-in utility functions

pdf.saveToFile() — Saves the generated PDF to a file.

pdf.saveToFile(filePath: String): Function
  • If the filePath has no extension, the .pdf extension will be automatically appended.
  • The filePath path is resolved regarding the current working directory.

Exemple:

client.convertHtml('<h1>Hello world</h1>')
    .then(pdf.saveToFile('/path/to/my-file.pdf'))
    .catch((err) => { console.log(err); });

pdf.sendHttpResponse() — Returns the generated PDF in an HTTP response.

pdf.sendHttpResponse(response: http.ServerResponse, fileName: String): Function
  • If the fileName has no extension, the .pdf extension will be automatically appended.

Exemple:

const express = require('express');
const app = express();
 
app.get('/pdf', (request, response) => {
    client.convertHtml('<h1>Hello world</h1>')
        .then(pdf.sendHttpResponse(response, 'my-file'))
        .catch((err) => { console.log(err); });
});
 
app.listen(3000);

Useful links

Readme

Keywords

none

Package Sidebar

Install

npm i pdfkiwi

Weekly Downloads

0

Version

0.1.1

License

MIT

Last publish

Collaborators

  • donov4n