Easy PDF
Build for Web and Backend 💪
If this package helped you out please star us on Github!
Much appreciated!
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)
var pdf = require('easypdf-io');
var html = '<p>Hello world!</p>';
var data = {
html: btoa(html),
background: "https://public.easypdf-io.io/img/watermark-draft.jpg",
settings: {
},
};
pdf.create(data, function (result) {
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 = {
background: "iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg=="
};
Local File (NodeJS only)
var fs = require("fs");
var pdf = require('easypdf-io');
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);
});
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);
Render(view) your PDF (browser only)
html
<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>
<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);