Skip to content

dveldhoen/easypdf-io

Repository files navigation

Easy PDF

Build for Web and Backend 💪


Version Build Status Downloads
License Pull Request's Welcome

If this package helped you out please star us on Github!
Much appreciated!

Pull Request's Welcome

Demo

JS Fiddle: Plain Javascript

Installing

Using npm:

$ npm install easypdf-io --save

Using yarn:

$ yarn add easypdf-io

Using unkpg CDN:

<script src="https://unpkg.com/easypdf-io/dist/easypdf-io.min.js"></script>

Import

Html

<script src="https://unpkg.com/easypdf-io/dist/easypdf-io.min.js"></script>

CommonJS

var pdf = require('easypdf-io');

ES6

import pdf from 'easypdf-io';

Direct REST API access

# HTTPS POST 
https://api.easypdf.io/v2/free/pdf

# POST Data
Format: JSON
Structure: {"data":{"html":""}} # Parent object must be 'data'

Example (NodeJS)

//Import the library into your project
var pdf = require('easypdf-io');

// Prepare your PDF content using HTML
var html = '<p>Hello world!</p>';

var data = {
    // btoa === base64 encode
    html: btoa(html), // Must be base64 encoded html. This example contains 'Hello World!' in base64
    background: "https://public.easypdf-io.io/img/watermark-draft.jpg",
    settings: {
        // "margin-top": 25, // Default to 25
        // "margin-right": 25, // Default to 25
        // "margin-left": 25, // Default to 25
        // "margin-bottom": 25, // Default to 25
        // "format": "Letter" // Defaults to A4, options: A3, A4, A5, Legal, Letter, Tabloid
    },
};

//Create your PDF! Easy!
pdf.create(data, function (result) {
    // The response will contain a base64 encoded PDF file
    // Using the below line we can decode the base64 and store the file locally
    fs.writeFileSync("sample.pdf", result.pdf, 'base64');
});

Return values

Key Value Data Type
result.pdf The PDF file as base64 string String

Background

The background and url inputs accept either a URL or a base64 encoded file.

Supported file types:

  • JPG, PNG, etc. (most common image types)
  • PDF

URL

const data = {
    background: "https://public.easypdf.io/img/watermark_draft.jpg"
};

Base64

const data = {
    //Note: Sample base64 string
    //Please use the link below to convert your image to base64
    background: "iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg=="
};

Local File (NodeJS only)

//Import fs to be able to read from the local file system
var fs = require("fs");
var pdf = require('easypdf-io');

//Use the code below to read your local file as a base64 string
const data = {
    background: fs.readFileSync('images/background.png', 'base64')
};

Click here for an online tool to convert an image to base64

Async/await support

var fs = require('fs');
var pdf = require('easypdf-io');

var html = btoa('<p>Hello world!</p>');
const result = await pdf.create({html});
await fs.writeFileSync("sample.pdf", result.pdf, 'base64');

Download your pdf (browser only)

Using callback

var pdf = require('easypdf-io');

var html = btoa('<p>Hello world!</p>');
pdf.create({html}, function (result) {
    pdf.download('sample.pdf', result.pdf);
    //	you can download like this as well:
    //	pdf.download();
    //	pdf.download('sample.pdf');   
});

Using async/await

var pdf = require('easypdf-io');

var html = btoa('<p>Hello world!</p>');
const result = await pdf.create({html});
pdf.download('sample.pdf', result.pdf);
//	you can download like this as well:
//	pdf.download();
//	pdf.download('sample.pdf');

Render(view) your PDF (browser only)

html

<!-- Only include when rendering is required -->
<script src="https://unpkg.com/pdfjs-dist/build/pdf.min.js"></script>
<script src="https://unpkg.com/pdfjs-dist/build/pdf.worker.min.js"></script>

<!-- Include pdfjs version 2.3.200 for Internet Explorer compatibility, no worker required -->
<!-- <script src="https://unpkg.com/[email protected]/build/pdf.min.js"></script> -->

<!-- The pdf will be rendered within this div -->
<div id="pdf"></div>

css (optional)

#pdf {
    text-align: center;
}

#pdf canvas {
    border: 1px solid black;
    width: 95%;
}

js: Using Callback

var html = btoa('<p>Hello world!</p>');
var elementId = 'pdf';
pdf.create({html}, function (result) {
    pdf.render(elementId, result.pdf, function () {
        console.log('PDF rendered!');
    });
});

js: Using async/await

var html = btoa('<p>Hello world!</p>');
const elementId = 'pdf';
const result = await pdf.create({html});
await pdf.render(elementId, result.pdf);

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published