0.1.1 • Published 8 years ago
pdfkiwi v0.1.1
pdfkiwi-node
A node library for interacting with pdf.kiwi API
Installation
# - Npm
npm install pdfkiwi --save
# - Yarn
yarn add pdfkiwiUsage
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 optionsparameters) 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 Buffercontaining 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): PromiseExemple:
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 filePathhas no extension, the.pdfextension will be automatically appended.
- The filePathpath 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 fileNamehas no extension, the.pdfextension 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
- https://pdf.kiwi
- https://doc.pdf.kiwi — API Documentation