Build for Web and Backend 💪
If this package helped you out please star us on Github!
Much appreciated!
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>
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';
# HTTPS POST
https://api.easypdf.io/v2/free/pdf
# POST Data
Format: JSON
Structure: {"data":{"html":""}} # Parent object must be 'data'
//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');
});
Key | Value | Data Type |
---|---|---|
result.pdf | The PDF file as base64 string | String |
The background and url inputs accept either a URL or a base64 encoded file.
Supported file types:
- JPG, PNG, etc. (most common image types)
const data = {
background: "https://public.easypdf.io/img/watermark_draft.jpg"
};
const data = {
//Note: Sample base64 string
//Please use the link below to convert your image to base64
background: "iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg=="
};
//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
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');
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');
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);