@anvilco/anvil
Advanced tools
Comparing version 2.4.0 to 2.5.0
@@ -10,2 +10,15 @@ # Changelog | ||
## [v2.5.0](https://github.com/anvilco/node-anvil/compare/v2.3.0...v2.5.0) | ||
### Merged | ||
- Add generatePDF() function [`#30`](https://github.com/anvilco/node-anvil/pull/30) | ||
- v2.4.0 [`#29`](https://github.com/anvilco/node-anvil/pull/29) | ||
### Commits | ||
- Add generate-pdf.js example with default data [`c1face8`](https://github.com/anvilco/node-anvil/commit/c1face8232650edacbc255f5148cc6871114270e) | ||
- Add generatePDF function and related tests [`135a643`](https://github.com/anvilco/node-anvil/commit/135a643cb1b59e76bdfdcf2f8c09ff1bad10c37d) | ||
- Add generatePDF docs to readme [`7ee5d09`](https://github.com/anvilco/node-anvil/commit/7ee5d0969e2f2bb2d54ae0a6c3899c487fa9a90a) | ||
## [v2.4.0](https://github.com/anvilco/node-anvil/compare/v2.3.0...v2.4.0) | ||
@@ -12,0 +25,0 @@ |
{ | ||
"name": "@anvilco/anvil", | ||
"version": "2.4.0", | ||
"version": "2.5.0", | ||
"description": "Anvil API Client", | ||
@@ -5,0 +5,0 @@ "main": "src/index.js", |
@@ -66,5 +66,5 @@ # Anvil API Client for Node | ||
Fills a PDF with your JSON data. | ||
Fills a PDF template with your JSON data. | ||
First, you will need to have [uploaded a PDF to Anvil](https://useanvil.com/api/fill-pdf). You can find the PDF template's id on the `API Info` tab of your PDF template's page: | ||
First, you will need to have [uploaded a PDF to Anvil](https://useanvil.com/docs/api/fill-pdf#creating-a-pdf-template). You can find the PDF template's id on the `API Info` tab of your PDF template's page: | ||
@@ -76,3 +76,7 @@ <img width="725" alt="pdf-template-id" src="https://user-images.githubusercontent.com/69169/73693549-4a598280-468b-11ea-81a3-5df4472de8a4.png"> | ||
```js | ||
const fs = require('fs') | ||
// PDF template you uploaded to Anvil | ||
const pdfTemplateID = 'kA6Da9CuGqUtc6QiBDRR' | ||
// Your API key from your Anvil organization settings | ||
@@ -96,2 +100,5 @@ const apiKey = '7j2JuUWmN4fGjBxsCltWaybHOEy3UEtt' | ||
const { statusCode, data } = await anvilClient.fillPDF(pdfTemplateID, payload, options) | ||
// Be sure to write the file as raw bytes | ||
fs.writeFileSync('filled.pdf', data, { encoding: null }) | ||
``` | ||
@@ -114,2 +121,57 @@ | ||
##### generatePDF(payload[, options]) | ||
Dynamically generate a new PDF with your JSON data. Useful for agreements, invoices, disclosures, or any other text-heavy documents. This does not require you do anything in the Anvil UI other than setup your API key, just send it data, get a PDF. See [the generate PDF docs](https://useanvil.com/api/generate-pdf) for full details. | ||
An example: | ||
```js | ||
const fs = require('fs') | ||
// Your API key from your Anvil organization settings | ||
const apiKey = '7j2JuUWmN4fGjBxsCltWaybHOEy3UEtt' | ||
// JSON data for the new PDF | ||
const payload = { | ||
title: 'Example Invoice', | ||
data: [{ | ||
label: 'Name', | ||
content: 'Sally Jones', | ||
}, { | ||
content: 'Lorem **ipsum** dolor sit _amet_', | ||
}, { | ||
table: { | ||
firstRowHeaders: true, | ||
rows: [ | ||
['Description', 'Quantity', 'Price'], | ||
['4x Large Widgets', '4', '$40.00'], | ||
['10x Medium Sized Widgets in dark blue', '10', '$100.00'], | ||
['10x Small Widgets in white', '6', '$60.00'], | ||
], | ||
}, | ||
}], | ||
} | ||
// The 'options' parameter is optional | ||
const options = { | ||
"dataType": "buffer" | ||
} | ||
const anvilClient = new Anvil({ apiKey }) | ||
const { statusCode, data } = await anvilClient.generatePDF(payload, options) | ||
// Be sure to write the file as raw bytes | ||
fs.writeFileSync('generated.pdf', data, { encoding: null }) | ||
``` | ||
* `payload` (Object) - The JSON data that will fill the PDF template | ||
* `title` (String) - _optional_ Set the title encoded into the PDF document | ||
* `data` (Array of Objects) - The data that generates the PDF. See [the docs](https://useanvil.com/docs/api/generate-pdf#supported-format-of-data) for all supported objects | ||
* For example `[{ "label": "Hello World!", "content": "Test" }]` | ||
* `options` (Object) - _optional_ Any additional options for the request | ||
* `dataType` (Enum[String]) - _optional_ Set the type of the `data` value that is returned in the resolved `Promise`. Defaults to `'buffer'`, but `'stream'` is also supported. | ||
* Returns a `Promise` that resolves to an `Object` | ||
* `statusCode` (Number) - the HTTP status code; `200` is success | ||
* `data` (Buffer | Stream) - The raw binary data of the filled PDF if success. Will be either a Buffer or a Stream, depending on `dataType` option supplied to the request. | ||
* `errors` (Array of Objects) - Will be present if status >= 400. See Errors | ||
* `message` (String) | ||
##### createEtchPacket(options) | ||
@@ -192,3 +254,3 @@ | ||
See the [Anvil API docs](https://useanvil.com/api/fill-pdf) for more information on the specifics of the rate limits. | ||
See the [Anvil API docs](https://useanvil.com/docs/api/fill-pdf) for more information on the specifics of the rate limits. | ||
@@ -199,3 +261,3 @@ ## API Documentation | ||
See the [PDF filling API docs](https://useanvil.com/api/fill-pdf) for more information about the `fillPDF` method. | ||
See the [PDF filling API docs](https://useanvil.com/docs/api/fill-pdf) for more information about the `fillPDF` method. | ||
@@ -202,0 +264,0 @@ ## Examples |
@@ -41,5 +41,5 @@ // https://www.npmjs.com/package/node-fetch#fetch-options | ||
// Local Dev | ||
// const apiKey = 'AXZj09jvHRPNBFRHMnAF2jpwOx9J5Rb6' | ||
const apiKey = 'AXZj09jvHRPNBFRHMnAF2jpwOx9J5Rb6' | ||
// Prod Mango Dev | ||
const apiKey = 'oBU3hec1pdBRGB5NmSJaj7QQzhiME1u8' | ||
// const apiKey = 'oBU3hec1pdBRGB5NmSJaj7QQzhiME1u8' | ||
@@ -70,3 +70,3 @@ // const jsonPath = '' | ||
// cookie: 'koa:sess=c2a23c802c7c9e09016378f74ef1ee1d; koa:sess.sig=PO0AfdOUgUcsLFhFLbaz7QgGncU', | ||
// baseURL, | ||
baseURL, | ||
// userAgent, | ||
@@ -73,0 +73,0 @@ debug: true, |
@@ -116,2 +116,25 @@ const fs = require('fs') | ||
generatePDF (payload, clientOptions = {}) { | ||
const supportedDataTypes = [DATA_TYPE_STREAM, DATA_TYPE_BUFFER] | ||
const { dataType = DATA_TYPE_BUFFER } = clientOptions | ||
if (dataType && !supportedDataTypes.includes(dataType)) { | ||
throw new Error(`dataType must be one of: ${supportedDataTypes.join('|')}`) | ||
} | ||
return this.requestREST( | ||
'/api/v1/generate-pdf', | ||
{ | ||
method: 'POST', | ||
body: JSON.stringify(payload), | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
}, | ||
{ | ||
...clientOptions, | ||
dataType, | ||
}, | ||
) | ||
} | ||
createEtchPacket ({ variables, responseQuery, mutation }) { | ||
@@ -118,0 +141,0 @@ return this.requestGraphQL( |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
115387
848
285