Skip to main content

Posts

Showing posts from August, 2020

fill in checkbox with pdf-lib

https://github.com/Hopding/pdf-lib/issues/109
constfooCheckBox=findAcroFieldByName(pdfDoc,'FooCheckBox');constbarCheckBox=findAcroFieldByName(pdfDoc,'BarCheckBox');constquxCheckBox=findAcroFieldByName(pdfDoc,'QuxCheckBox');constbazCheckBox=findAcroFieldByName(pdfDoc,'BazCheckBox');console.log('Selecting "Foo" and "Qux" boxes...');fooCheckBox.set('V',PDFName.from('Yes'));

turn GraphQL mutation into promise - with parameters

exportinterfaceSigningProps { forms: Document[] todos?: Document[] listingId: string listingAddress: Address addDocument?: (data: DocumentInput) =>void printPdf?: (data: PrintPdfInput) =>void fillInPdfForm: (data: FillInPdfInput) =>Promise<string> deleteTodo?: (id: string) =>void } functionfillInPdfForm(fillInPdfInput: FillInPdfInput): Promise<string> {returnnewPromise((resolve, reject) => { fillInPdf({ variables: { fillInPdfInput:fillInPdfInput, }, }) .then((data: any) => { constdoc = { ...data.data.fillInPdf, created:newDate().toString(), } setDocuments([...documents, doc]) consttodoDocs = todoDocuments.filter(todo=>todo.name !== doc.name) setTodoDocuments(todoDocs) resolve('') }) .catch((error: any) => { console.log(error) reject(error) }) }) }

useEffect(() => { constfillInPdf = async () => { try { letfieldNames: string[] = [] letfieldValues: string[] = [] if (formValues) { for (constnameinformVal…

pdf-lib node package for manipulate pdf template

use pdf-lib to fill in pdf template:
export const fillForm = async (pdfDoc: any, fieldNames: string[], fieldValues: string[]) =&gt; {  const form = pdfDoc.context.lookup(pdfDoc.catalog.get(PDFName.of('AcroForm')))  if (!form) {    throw new Error('PDF does not contain a form')  }
  form.set(PDFName.of('NeedAppearances'), PDFBool.True)
  const fieldRefs = form.context.lookup(form.get(PDFName.of('Fields')))  if (!fieldRefs) {    throw new Error('PDF form does not contain any fields')  }
  const fields = fieldRefs.array.map((ref: any) =&gt; form.context.lookup(ref))
  fields.forEach((field: any) =&gt; {    const name = field.get(PDFName.of('T'))    console.log('pdf form fieldName:', name, name.value)    if (name) {      const idx = fieldNames.indexOf(name.value)      const newValue = idx &gt;= 0 ? fieldValues[idx] : ''       if (newValue) {        field.set(PDFName.of('V'), PDFString.of(newValue))        f…

convert callback to async/await with promise

1) Node version 8 and above now support turning callback functions into promises using the built-in util module.
const request = require('request'); const util = require('util'); const fetchData = util.promisify(request); fetchData(url).then(data => { let content = JSON.parse(data.body); console.log('Joke: ', content.value); }).catch(err => console.log('error: ', err))2) Turn callback into a promise export const mergePdfs = async (pdf1: string, pdf2: string, pdf: string) => { return new Promise((resolve, reject) => { merge([pdf1, pdf2], pdf, (error: any) => { if (error) { return reject(error) } resolve('') }) }) }
3) sample for printPDF

check variable undefined with quokka

// listingId = '', 0, null, undefined, false //let listingId
if (typeoflistingId === 'undefined') console.log('listingId is undefined') else console.log(listingId)

if (!!listingId) console.log(listingId) else console.log('listingId does not found')
if (listingId) console.log(listingId) else console.log('listingId does not found')

if (listingId === undefined) console.log('listingId does not found') else console.log(listingId)

add Digital Signature to pdf with pdfKit and react-signature-canvas for NodeJS / GraphQL

to create hand-craft signature with signaturePad, add date and save to a pdf. then add signature PDF as an additional page attach to the main pdf.

https://www.npmjs.com/package/react-signature-canvas
** need remove alpha from image in order for pdfKit to create pdf by: 
1. set background color to whte
<SignaturePad ref={sigPad} backgroundColor="rgba(255,255,255,1)" canvasProps={{ className:classes.sigPad }} />

2. to image/jpeg constimageData = sigPad.current .getTrimmedCanvas() .toDataURL('image/jpeg')
3. merge pdfs using https://www.npmjs.com/package/easy-pdf-merge
exportconstmergePdfs = async (pdf1: string, pdf2: string, pdf: string) => { returnnewPromise((resolve, reject) => { merge([pdf1, pdf2], pdf, (error: any) => { if (error) { returnreject(error) } resolve('') }) }) } ====
Here are other ways to sign PDF:
https://www.youtube.com/watch?v=pUIWvJgkw8E
Node Library: https://www.npmjs.com/package/node-signpdf

importsignerf…