import _ from './util' ; import { Draw } from './draw' ; import { changeDpiDataUrl } from './changedpi' ; import { tr } from './translations' ; import { Store } from './store' ; function exportInstructions ( app , store , exportType , hiResScale , drawPageCallback , doneCallback ) { app . busyText = tr ( `dialog.busy_indicator.generating_${ exportType }` ) ; async function exportPage ( page , canvas ) { return new Promise ( resolve => window . setTimeout ( ( ) => { if ( ! page ) { resolve ( ) ; return ; } if ( page . needsLayout ) { store . mutations . page . layout ( { page } ) ; } Draw . page ( page , canvas , { hiResScale , noCache : true , noGrid : true } ) ; drawPageCallback ( page , canvas ) ; app . updateProgress ( `Page ${ page . number || 0 }` ) ; resolve ( ) ; } , 100 ) ) ; } async function exportPages ( pages , canvas ) { for ( let i = 0 ; i < pages . length ; i ++ ) { await exportPage ( pages [ i ] , canvas ) ; } } window . setTimeout ( ( ) => { const start = Date . now ( ) ; const canvas = document . getElementById ( 'exportImagesCanvas' ) as HTMLCanvasElement ; canvas . width = ( store . state ?. template ?. page ?. width ?? 0 ) * hiResScale ; canvas . height = ( store . state ?. template ?. page ?. height ?? 0 ) * hiResScale ; const pages = store . state . pages . slice ( 1 ) ; app . updateProgress ( { stepCount : pages . length , text : 'Page 0' } ) ; exportPages ( pages , canvas ) . then ( ( ) => { doneCallback ( ( ) => { app . updateProgress ( { clear : true } ) ; const end = Date . now ( ) ; const time = _ . formatTime ( start , end ) ; app . statusText = tr ( `action.export.${ exportType . toLowerCase ( ) }.success_message_@mf` , { exportType , time } ) ; } ) ; } ) ; } , 0 ) ; } interface PDFConfig { dpi : number ; pageSize : { width : number ; height : number ; } ; } function generatePDF ( app , store , config ? ) { let hiResScale = 1 ; const pageSize = { width : store . state . template . page . width * 0.75 , height : store . state . template . page . height * 0.75 , } ; if ( config ) { hiResScale = config . dpi / 96 ; hiResScale = hiResScale * config . pageSize . width / pageSize . width ; pageSize . width = config . pageSize . width ; pageSize . height = config . pageSize . height ; } const doc = new jsPDF ( pageSize . width > pageSize . height ? 'landscape' : 'portrait' , 'pt' , [ pageSize . width , pageSize . height ] ) ; function drawPage ( page , canvas ) { const data = canvas . toDataURL ( 'image/jpeg' ) ; doc . addImage ( data , 'JPEG' , 0 , 0 , pageSize . width , pageSize . height ) ; if ( ! store . get . isLastPage ( page ) ) { doc . addPage ( pageSize . width , pageSize . height ) ; } } function done ( finishedCallback ) { doc . save ( store . get . modelFilenameBase ( '.pdf' ) ) ; finishedCallback ( ) ; } exportInstructions ( app , store , 'PDF' , hiResScale , drawPage , done ) ; } function generatePNGZip ( app , store , hiResScale = 1 , dpi = 96 ) { const fn = store . get . modelFilenameBase ( ) ; const zip = new JSZip ( ) ; const imgFolder = zip . folder ( fn ) ; function drawPage ( page , canvas ) { const pageName = ( page . subtype === 'titlePage' ) ? 'Page 1 Title Page.png' : `Page ${ page . number }.png` ; let data = canvas . toDataURL ( ) ; if ( dpi !== 96 ) { data = changeDpiDataUrl ( data , dpi ) ; } data = data . substr ( data . indexOf ( ',' ) + 1 ) ; imgFolder . file ( pageName , data , { base64 : true } ) ; } function done ( finishedCallback ) { zip . generateAsync ( { type : 'blob' } ) . then ( ( content ) => saveAs ( content , fn + '.zip' ) ) . then ( finishedCallback ) ; } exportInstructions ( app , store , 'PNG' , hiResScale , drawPage , done ) ; } export default { generatePDF , generatePNGZip } ;