1.1.0 • Published 3 years ago

pdfbix v1.1.0

Weekly downloads
5
License
GPL-3.0
Repository
github
Last release
3 years ago

Pdfbix - Convert HTML & URL to High Quality PDF

NPM version NPM Licence Snyk Vulnerabilities js-standard-style NPM downloads

Nodejs client SDK for Pdfbix

Pdfbix is one of the cheapest tool to convert your HTML or URL to pdf. You can generate pdf at the cost of setting up your own server. It's a tool developed by developers for developer so that they can focus on business logic & forget about all the hassle of maintaing & scaling servers for generating pdf.

Installation

Download the NPM module

npm install pdfbix --save

Authorization

Create an account at Pdfbix to get your API key.

Usage

Initialize the Client

Require the package in your code & create a pdfbix object.

const { Pdfbix } = require('pdfbix');
const pdfbix = new Pdfbix({ authKey: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' });

All options that can be passed while creating object

KeyValueRequired/OptionalDefaultDescription
authKeyStringrequiredRequired to validate request made via sdk
baseUrlStringoptionalhttps://api.pdfbix.comUsed as base URL for all calls
apiVersionStringoptionalv1Changed when breaking changes are added

Convert URL to PDF

// All options mentioned later in doc
const args = { fileName: 'example.pdf' }

pdfbix.convertUrlToPdf('https://example.com', args).then(res => {
    console.log(res)
}).catch(error => {
    console.log(error)
})

Convert HTML to PDF

// All options mentioned later in doc
const args = { fileName: 'example.pdf' }

pdfbix.convertHtmlToPdf('https://example.com', args).then(res => {
    console.log(res)
}).catch(error => {
    console.log(error)
})

Async-Await Implementation

Above result can also be achieved by using async & await

try {
    const result = await pdfbix.convertUrlToPdf('https://example.com', args)
} catch (error) {
    console.log(error)
}

All options that can be passed in args

KeyValueRequired/OptionalDefaultDescription
toUrlBooleanoptionaltruetrue: return a link to download pdf; false: return base64 representation of pdf
fileNameStringoptional{requestId}_{currentDatetime}Custom file name given to file
enableCustomStorageBooleanoptionalfalseIf enabled all files will be saved to configured s3 bucket in dashboard
customStorageStringoptionalWill only work if enableCustomStorage is enabled. Valid options are aws and gcp
pdfOptsObjectoptional{}Puppeteer options that can be passed, to configure final pdf

All options that can be passed into pdfOpts can be found here

Responses

It's recommended to enclose pdfbix call into try-catch block to handle unexpected response. Here are possible result format you can expect.

Success Response

{
  "statusCode": 200,
  "data": {
    "content": "https://pdfbix.s3.ap-south-1.amazonaws.com/8Cdq8LGPQOSF1vUpTlOwqQ-0000002962-1610833500980.pdf",
    "isBase64Encoded": false,
    "fileSize": 35.14258,
    "timeTaken": 6.409,
    "cost": 0.00446
  }
}

If isBase64Encoded is true then content will contain base64 string representing final pdf, you might need to parse that base64 String. isBase64Encoded will be true when you set toUrl as false in args while calling convertHtmlToPdf or convertUrlToPdf.

Error Response

{
  "statusCode": 401,
  "error": {
    "name": "BackendClientError",
    "code": "ERR_INVALID_TOKEN",
    "message": "Token you're passing is invalid.",
    "statusCode": 401,
    "errorData": {}
  },
  "message": "Token you're passing is invalid."
}

Possible Error Code

CodeStatus CodeDescription
ERR_INVALID_TOKEN401When token you're passing is not correct
BAD_REQUEST_DATA400Sent for any request which server can't parse or any required parameter is missing
NO_AUTH_KEY411When you're not passing any authKey while initializing SDK
NEGATIVE_BALANCE415Balance is negative in your wallet, Please recharge
INACTIVE_ACCOUNT416Account is inactive or suspended by Admin
BLOCKED_ACCOUNT417Account is blocked by Admin
INACTIVE_APPLICATION418Application is inactive or suspended by Admin
BLOCKED_APPLICATION419Application is blocked by Admin
NO_URL_PROVIDED421You forget to pass url to function convertUrlToPdf
NO_HTML_PROVIDED422You forget to pass HTML String to function convertHtmlToPdf
NO_STORAGE_CONFIGURED427You need to configure your custom storage first (currently S3 supported)
EMAIL_NOT_VERIFIED428Email is not verified. Please verify your email

Custom Storage Configuration

Enable s3 Storage

It's very easy to confugure your own s3 bucket, you just need to generate your aws programmatic keys. Its recommended to create a separate user for your project with right S3 programmatic access.

Enable google cloud Storage

You need to create bucket in GCP using console or cli. To do so, you can follow the steps mentioned here.

To enable upload using code you need to genreate credentials or a role with storage.objects.create permission. You can follow the steps in this guide to upload objects into bucket.

Note: After you have generated your keys you can configure them on our Customer panel under Account -> Custom Storage. Your keys are stored in encrypted format, So you don't have to worry about any privacy loss.