diff --git a/examples/create-etch-packet.js b/examples/create-etch-packet.js new file mode 100644 index 0000000..3899693 --- /dev/null +++ b/examples/create-etch-packet.js @@ -0,0 +1,46 @@ +// Create an e-sign packet with the Anvil API. +// https://www.useanvil.com/docs/api/e-signatures +// +// Run: ANVIL_API_KEY= node examples/create-etch-packet.js +const Anvil = require('@anvilco/anvil') + +const pdfTemplateID = '05xXsZko33JIO6aq5Pnr' +const signerName = 'Testy Signer' +const signerEmail = process.argv[2] +const anvilClient = new Anvil({ apiKey: process.env.ANVIL_API_KEY }) + +if (!signerEmail) { + console.log('Usage: node examples/create-etch-packet.js ') + process.exit(1) +} + +const variables = { + isDraft: false, + // Test packets use development signatures and do not count toward billing. + isTest: true, + name: `Test Docs - ${signerName}`, + files: [{ id: 'sampleTemplate', castEid: pdfTemplateID }], + data: { + payloads: { + sampleTemplate: { data: { name: signerName, email: signerEmail } }, + }, + }, + signers: [{ + id: 'signer1', + name: signerName, + email: signerEmail, + signerType: 'email', + fields: [{ fileId: 'sampleTemplate', fieldId: 'signature' }], + }], +} + +async function main () { + const { data, errors } = await anvilClient.createEtchPacket({ variables }) + if (errors) { + console.log('Errors:', JSON.stringify(errors, null, 2)) + return + } + console.log('Packet created:', data.data.createEtchPacket.detailsURL) +} + +main() diff --git a/examples/create-etch-packet.ts b/examples/create-etch-packet.ts new file mode 100644 index 0000000..719e2db --- /dev/null +++ b/examples/create-etch-packet.ts @@ -0,0 +1,46 @@ +// Create an e-sign packet with the Anvil API. +// https://www.useanvil.com/docs/api/e-signatures +// +// Run: ANVIL_API_KEY= yarn ts-node examples/create-etch-packet.ts +import Anvil, { GraphQLResponse } from '@anvilco/anvil' + +const pdfTemplateID = '05xXsZko33JIO6aq5Pnr' +const signerName = 'Testy Signer' +const signerEmail = process.argv[2] ?? '' +const anvilClient = new Anvil({ apiKey: process.env['ANVIL_API_KEY'] ?? '' }) + +if (!signerEmail) { + console.log('Usage: yarn ts-node examples/create-etch-packet.ts ') + process.exit(1) +} + +const variables = { + isDraft: false, + // Test packets use development signatures and do not count toward billing. + isTest: true, + name: `Test Docs - ${signerName}`, + files: [{ id: 'sampleTemplate', castEid: pdfTemplateID }], + data: { + payloads: { + sampleTemplate: { data: { name: signerName, email: signerEmail } }, + }, + }, + signers: [{ + id: 'signer1', + name: signerName, + email: signerEmail, + signerType: 'email', + fields: [{ fileId: 'sampleTemplate', fieldId: 'signature' }], + }], +} + +async function main () { + const { data, errors }: GraphQLResponse = await anvilClient.createEtchPacket({ variables }) + if (errors) { + console.log('Errors:', JSON.stringify(errors, null, 2)) + return + } + console.log('Packet created:', data?.data['createEtchPacket'].detailsURL) +} + +main() diff --git a/examples/fill.js b/examples/fill.js new file mode 100644 index 0000000..7259b3a --- /dev/null +++ b/examples/fill.js @@ -0,0 +1,35 @@ +// Fill a PDF template with the Anvil API. +// https://www.useanvil.com/docs/api/fill-pdf +// +// Run: ANVIL_API_KEY= node examples/fill.js +const fs = require('fs') +const Anvil = require('@anvilco/anvil') + +// Sample template available to anyone; make your own at +// https://www.useanvil.com/help/tutorials/set-up-a-pdf-template +const pdfTemplateID = '05xXsZko33JIO6aq5Pnr' +const anvilClient = new Anvil({ apiKey: process.env.ANVIL_API_KEY }) + +const exampleData = { + title: 'My PDF Title', + fontSize: 10, + textColor: '#333333', + data: { + shortText: 'Hello World!', + name: { firstName: 'Robin', lastName: 'Smith' }, + email: 'robin@example.com', + }, +} + +async function main () { + const { statusCode, data, errors } = await anvilClient.fillPDF(pdfTemplateID, exampleData) + if (statusCode !== 200) { + console.log('Errors:', JSON.stringify(errors, null, 2)) + return + } + // `data` is the filled PDF binary; save with no encoding or the file corrupts. + fs.writeFileSync('fill-output.pdf', data, { encoding: null }) + console.log('Saved fill-output.pdf') +} + +main() diff --git a/examples/fill.ts b/examples/fill.ts new file mode 100644 index 0000000..8fdedcb --- /dev/null +++ b/examples/fill.ts @@ -0,0 +1,35 @@ +// Fill a PDF template with the Anvil API. +// https://www.useanvil.com/docs/api/fill-pdf +// +// Run: ANVIL_API_KEY= yarn ts-node examples/fill.ts +import fs from 'fs' +import Anvil from '@anvilco/anvil' + +// Sample template available to anyone; make your own at +// https://www.useanvil.com/help/tutorials/set-up-a-pdf-template +const pdfTemplateID = '05xXsZko33JIO6aq5Pnr' +const anvilClient = new Anvil({ apiKey: process.env['ANVIL_API_KEY'] ?? '' }) + +const exampleData = { + title: 'My PDF Title', + fontSize: 10, + textColor: '#333333', + data: { + shortText: 'Hello World!', + name: { firstName: 'Robin', lastName: 'Smith' }, + email: 'robin@example.com', + }, +} + +async function main () { + const { statusCode, data, errors } = await anvilClient.fillPDF(pdfTemplateID, exampleData) + if (statusCode !== 200 || !data) { + console.log('Errors:', JSON.stringify(errors, null, 2)) + return + } + // `data` is the filled PDF binary; save with no encoding or the file corrupts. + fs.writeFileSync('fill-output.pdf', data, { encoding: null }) + console.log('Saved fill-output.pdf') +} + +main() diff --git a/examples/generate-html.js b/examples/generate-html.js new file mode 100644 index 0000000..a29a855 --- /dev/null +++ b/examples/generate-html.js @@ -0,0 +1,35 @@ +// Generate a PDF from HTML and CSS with the Anvil API. +// https://www.useanvil.com/docs/api/generate-pdf +// +// Run: ANVIL_API_KEY= node examples/generate-html.js +const fs = require('fs') +const Anvil = require('@anvilco/anvil') + +const anvilClient = new Anvil({ apiKey: process.env.ANVIL_API_KEY }) + +const exampleData = { + title: 'Example HTML to PDF', + type: 'html', + data: { + html: ` +

What is Lorem Ipsum?

+

+ Lorem Ipsum is simply dummy text of the printing and typesetting + industry, and has been the standard ever since the 1500s. +

+ `, + css: 'body { font-size: 14px; color: #171717; }', + }, +} + +async function main () { + const { statusCode, data, errors } = await anvilClient.generatePDF(exampleData) + if (statusCode !== 200) { + console.log('Errors:', JSON.stringify(errors, null, 2)) + return + } + fs.writeFileSync('generate-html-output.pdf', data, { encoding: null }) + console.log('Saved generate-html-output.pdf') +} + +main() diff --git a/examples/generate-html.ts b/examples/generate-html.ts new file mode 100644 index 0000000..6ae7ba6 --- /dev/null +++ b/examples/generate-html.ts @@ -0,0 +1,35 @@ +// Generate a PDF from HTML and CSS with the Anvil API. +// https://www.useanvil.com/docs/api/generate-pdf +// +// Run: ANVIL_API_KEY= yarn ts-node examples/generate-html.ts +import fs from 'fs' +import Anvil from '@anvilco/anvil' + +const anvilClient = new Anvil({ apiKey: process.env['ANVIL_API_KEY'] ?? '' }) + +const exampleData = { + title: 'Example HTML to PDF', + type: 'html', + data: { + html: ` +

What is Lorem Ipsum?

+

+ Lorem Ipsum is simply dummy text of the printing and typesetting + industry, and has been the standard ever since the 1500s. +

+ `, + css: 'body { font-size: 14px; color: #171717; }', + }, +} + +async function main () { + const { statusCode, data, errors } = await anvilClient.generatePDF(exampleData) + if (statusCode !== 200 || !data) { + console.log('Errors:', JSON.stringify(errors, null, 2)) + return + } + fs.writeFileSync('generate-html-output.pdf', data, { encoding: null }) + console.log('Saved generate-html-output.pdf') +} + +main() diff --git a/examples/generate-markdown.js b/examples/generate-markdown.js new file mode 100644 index 0000000..3c9c545 --- /dev/null +++ b/examples/generate-markdown.js @@ -0,0 +1,39 @@ +// Generate a PDF from Markdown with the Anvil API. +// https://www.useanvil.com/docs/api/generate-pdf +// +// Run: ANVIL_API_KEY= node examples/generate-markdown.js +const fs = require('fs') +const Anvil = require('@anvilco/anvil') + +const anvilClient = new Anvil({ apiKey: process.env.ANVIL_API_KEY }) + +const exampleData = { + title: 'Example Invoice', + data: [{ + label: 'Name', + content: 'Sally Jones', + }, { + content: 'Lorem **ipsum** dolor sit _amet_, consectetur adipiscing elit.', + }, { + table: { + firstRowHeaders: true, + rows: [ + ['Description', 'Quantity', 'Price'], + ['4x Large Widgets', '4', '$40.00'], + ['10x Medium Widgets', '10', '$100.00'], + ], + }, + }], +} + +async function main () { + const { statusCode, data, errors } = await anvilClient.generatePDF(exampleData) + if (statusCode !== 200) { + console.log('Errors:', JSON.stringify(errors, null, 2)) + return + } + fs.writeFileSync('generate-markdown-output.pdf', data, { encoding: null }) + console.log('Saved generate-markdown-output.pdf') +} + +main() diff --git a/examples/generate-markdown.ts b/examples/generate-markdown.ts new file mode 100644 index 0000000..04aa139 --- /dev/null +++ b/examples/generate-markdown.ts @@ -0,0 +1,39 @@ +// Generate a PDF from Markdown with the Anvil API. +// https://www.useanvil.com/docs/api/generate-pdf +// +// Run: ANVIL_API_KEY= yarn ts-node examples/generate-markdown.ts +import fs from 'fs' +import Anvil from '@anvilco/anvil' + +const anvilClient = new Anvil({ apiKey: process.env['ANVIL_API_KEY'] ?? '' }) + +const exampleData = { + title: 'Example Invoice', + data: [{ + label: 'Name', + content: 'Sally Jones', + }, { + content: 'Lorem **ipsum** dolor sit _amet_, consectetur adipiscing elit.', + }, { + table: { + firstRowHeaders: true, + rows: [ + ['Description', 'Quantity', 'Price'], + ['4x Large Widgets', '4', '$40.00'], + ['10x Medium Widgets', '10', '$100.00'], + ], + }, + }], +} + +async function main () { + const { statusCode, data, errors } = await anvilClient.generatePDF(exampleData) + if (statusCode !== 200 || !data) { + console.log('Errors:', JSON.stringify(errors, null, 2)) + return + } + fs.writeFileSync('generate-markdown-output.pdf', data, { encoding: null }) + console.log('Saved generate-markdown-output.pdf') +} + +main()