import pdfMake , { TCreatedPdf } from 'pdfmake/build/pdfmake' ; import { commonVFS } from '@client/pdfRenderer/common_vfs' ; import { transformers } from '@client/pdfRenderer/transformer' ; import { IPDFTemplate , OptionalData } from '@client/pdfRenderer/transformer/types' ; import { IntlShape } from 'react-intl' ; import { IApplication } from '@client/applications' ; import { IUserDetails } from '@client/utils/userUtils' ; import { IOfflineData } from '@client/offline/reducer' ; export function createPDF ( template , application , userDetails , offlineResource , intl , optionalData ? ) { pdfMake . vfs = { ... commonVFS , ... template . vfs } ; let definitionString = JSON . stringify ( template . definition ) ; if ( template . transformers && template . transformers . length > 0 ) { template . transformers . forEach ( ( transformerDef ) => { const transformFunction = transformers [ transformerDef . operation ] ; if ( ! transformFunction ) { throw new Error ( `No transform function found for given name: ${ transformerDef . operation }` ) ; } let result = transformFunction ( { application , userDetails , resource : offlineResource } , intl , transformerDef . parameters , optionalData ) ; if ( typeof transformerDef . valueIndex !== 'undefined' && typeof result === 'string' ) { result = ( result as string ) . charAt ( transformerDef . valueIndex ) || '' ; } definitionString = definitionString . replace ( new RegExp ( `{${ transformerDef . field }}` , 'gi' ) , result || '' ) ; } ) ; } return pdfMake . createPdf ( JSON . parse ( definitionString ) , undefined , template . fonts [ intl . locale ] ) ; } export function printPDF ( template , application , userDetails , offlineResource , intl , optionalData ? ) { createPDF ( template , application , userDetails , offlineResource , intl , optionalData ) . print ( ) ; }