Giter Site home page Giter Site logo

api2pdf / api2pdf.node Goto Github PK

View Code? Open in Web Editor NEW
41.0 4.0 6.0 31 KB

Node client library for the Api2Pdf.com REST API - Convert HTML to PDF, URL to PDF, Office Docs to PDF, Merge PDFs, HTML to Image, URL to Image, HTML to Docx, HTML to Xlsx, PDF to HTML, Thumbnail preview of office files

Home Page: https://www.api2pdf.com

License: MIT License

JavaScript 100.00%
pdf pdf-generation html-to-pdf url-to-pdf merge-pdf word-to-pdf wkhtmltopdf headless-chrome libreoffice aws-lambda nodejs rest-api barcode qrcode cloud-run pdf-to-html html-to-docx html-to-xlsx html-to-image url-to-image

api2pdf.node's Introduction

api2pdf.node

Nodejs client for Api2Pdf REST API

Api2Pdf.com is a powerful REST API for instantly generating PDF and Office documents from HTML, URLs, Microsoft Office Documents (Word, Excel, PPT), Email files, and images. You can generate image preview or thumbnail of a PDF, office document, or email file. The API also supports merge / concatenation of two or more PDFs, setting passwords on PDFs, and adding bookmarks to PDFs. Api2Pdf is a wrapper for popular libraries such as wkhtmltopdf, Headless Chrome, PdfSharp, and LibreOffice.

Installation

npm install --save api2pdf

Resources

Resources this API supports:

Authorization

Acquire API Key

Create an account at portal.api2pdf.com to get your API key.

Usage

Initialize the Client

All usage starts by requiring api2pdf and creating a new object.

var Api2Pdf = require('api2pdf');   
var a2pClient = new Api2Pdf('YOUR-API-KEY');

Once you initialize the client, you can make calls like so:

a2pClient.chromeUrlToPdf('https://www.github.com')
    .then(function(result) {
        console.log(result); //successful api call
    }, function(rejected) {
        console.log(rejected); //an error occurred
    }
);

Successful Result Format

{
    'FileUrl': 'https://link-to-file-only-available-for-24-hours',
    'MbOut': 0.08830547332763672,
    'Cost': 0.00017251586914062501,
    'Success': true,
    'Error': null,
    'ResponseId': '6e46637a-650d-46d5-af0b-3d7831baccbb'
}

Failed Result Format

{
    'Success': false,
    'Error': 'some reason for the error',
    'ResponseId': '6e46637a-650d-46d5-af0b-3d7831baccbb'
}

wkhtmltopdf

Convert HTML to PDF

a2pClient.wkHtmlToPdf('<p>Hello, World</p>').then(function(result) {
    console.log(result);
});

Convert HTML to PDF (download PDF as a file and specify a file name)

a2pClient.wkHtmlToPdf('<p>Hello, World</p>', { inline: false, filename: 'test.pdf' }).then(function(result) {
    console.log(result);
});

Convert HTML to PDF (use object for advanced wkhtmltopdf settings) View full list of wkhtmltopdf options available.

var options = { orientation: 'landscape', pageSize: 'A4'};
a2pClient.wkHtmlToPdf('<p>Hello, World</p>', { options: options }).then(function(result) {
    console.log(result);
});

Convert URL to PDF

a2pClient.wkUrlToPdf('https://www.github.com').then(function(result) {
    console.log(result);
});

Convert URL to PDF (download PDF as a file and specify a file name)

a2pClient.wkUrlToPdf('https://www.github.com', { inline: false, filename: 'test.pdf' }).then(function(result) {
    console.log(result);
});

Convert URL to PDF (use object for advanced wkhtmltopdf settings) View full list of wkhtmltopdf options available.

var options = { orientation: 'landscape', pageSize: 'A4'};
a2pClient.wkUrlToPdf('https://www.github.com', { options: options }).then(function(result) {
    console.log(result);
});

Headless Chrome

Convert HTML to PDF

a2pClient.chromeHtmlToPdf('<p>Hello, World</p>').then(function(result) {
    console.log(result);
});

Convert HTML to PDF (download PDF as a file and specify a file name)

a2pClient.chromeHtmlToPdf('<p>Hello, World</p>', { inline: false, filename: 'test.pdf' }).then(function(result) {
    console.log(result);
});

Convert HTML to PDF (use options for advanced Headless Chrome settings) View full list of Headless Chrome options available.

var options = { landscape: true };
a2pClient.chromeHtmlToPdf('<p>Hello, World</p>', { options: options }).then(function(result) {
    console.log(result);
});

Convert URL to PDF

a2pClient.chromeUrlToPdf('https://www.github.com').then(function(result) {
    console.log(result);
});

Convert URL to PDF (download PDF as a file and specify a file name)

a2pClient.chromeUrlToPdf('https://www.github.com', { inline: false, filename: 'test.pdf' }).then(function(result) {
    console.log(result);
});

Convert URL to PDF (use keyword arguments for advanced Headless Chrome settings) View full list of Headless Chrome options available.

var options = { landscape: true };
a2pClient.chromeUrlToPdf('https://www.github.com', { options: options }).then(function(result) {
    console.log(result);
});

Convert HTML to Image

a2pClient.chromeHtmlToImage('<p>Hello, World</p>').then(function(result) {
    console.log(result);
});

Convert URL to Image

a2pClient.chromeUrlToImage('https://www.github.com').then(function(result) {
    console.log(result);
});

LibreOffice

Convert any office file to PDF, image file to PDF, email file to PDF, HTML to Word, HTML to Excel, and PDF to HTML. Any file that can be reasonably opened by LibreOffice should be convertible. Additionally, we have an endpoint for generating a thumbnail of the first page of your PDF or Office Document. This is great for generating an image preview of your files to users.

You must provide a url to the file. Our engine will consume the file at that URL and convert it.

Convert Microsoft Office Document or Image to PDF

a2pClient.libreOfficeAnyToPdf('https://www.api2pdf.com/wp-content/themes/api2pdf/assets/samples/sample-word-doc.docx').then(function(result) {
    console.log(result);
});

Convert Microsoft Office Document or Image to PDF (download PDF as a file and specify a file name)

a2pClient.libreOfficeAnyToPdf('https://www.api2pdf.com/wp-content/themes/api2pdf/assets/samples/sample-word-doc.docx', { inline: false, filename: 'test.pdf' }).then(function(result) {
    console.log(result);
});

Thumbnail or Image Preview of a PDF or Office Document or Email file

a2pClient.libreOfficeThumbnail('https://www.api2pdf.com/wp-content/themes/api2pdf/assets/samples/sample-word-doc.docx').then(function(result) {
    console.log(result);
});

Convert HTML to Microsoft Word or Docx

a2pClient.libreOfficeHtmlToDocx('http://www.api2pdf.com/wp-content/uploads/2021/01/sampleHtml.html').then(function(result) {
    console.log(result);
});

Convert HTML to Microsoft Excel or Xlsx

a2pClient.libreOfficeHtmlToXlsx('http://www.api2pdf.com/wp-content/uploads/2021/01/sampleTables.html').then(function(result) {
    console.log(result);
});

Convert PDF to HTML

a2pClient.libreOfficePdfToHtml('http://www.api2pdf.com/wp-content/uploads/2021/01/1a082b03-2bd6-4703-989d-0443a88e3b0f-4.pdf').then(function(result) {
    console.log(result);
});

PdfSharp - Merge / Concatenate Two or More PDFs, Add bookmarks to pdfs, add passwords to pdfs

To use the merge endpoint, supply a list of urls to existing PDFs. The engine will consume all of the PDFs and merge them into a single PDF, in the order in which they were provided in the list.

Merge PDFs from list of URLs to existing PDFs

var urls = ['url-to-pdf1', 'url-to-pdf2'];
a2pClient.pdfsharpMerge(urls).then(function(result) {
    console.log(result);
});

Add bookmarks to existing PDF

var url = 'https://LINK-TO-PDF';
var bookmarks = [
    { Page: 0, Title: "Introduction"},
    { Page: 1, Title: "Second page"}
]
a2pClient.pdfsharpAddBookmarks(url, bookmarks).then(function(result) {
    console.log(result);
});

Add password to existing PDF

var url = 'https://LINK-TO-PDF';
var userpassword = "hello";
a2pClient.pdfsharpAddPassword(url, userpassword).then(function(result) {
    console.log(result);
});

Compress existing PDF

var url = 'https://LINK-TO-PDF';
a2pClient.pdfsharpCompress(url).then(function(result) {
    console.log(result);
});

Extract pages from existing PDF

var url = 'https://LINK-TO-PDF';
var start = "2";
var end = null;
a2pClient.pdfsharpExtractPages(url, start, end).then(function(result) {
    console.log(result);
});

Helper Methods

Delete a PDF on Command with delete(responseId)

By default, Api2Pdf will delete your generated file 24 hours after it has been generated. For those with high security needs, you may want to delete your file on command. You can do so by making an DELETE api call with the responseId attribute that was returned on the original JSON payload.

var responseId = result.ResponseId //from previous api call
a2pClient.utilityDelete(responseId);

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.