import { SurveyModel , Event , Question } from 'survey-core' ; import { IDocOptions , DocController } from './doc_controller' ; import { FlatSurvey } from './flat_layout/flat_survey' ; import { PagePacker } from './page_layout/page_packer' ; import { IPdfBrick } from './pdf_render/pdf_brick' ; import { EventAsync , EventHandler } from './event_handler/event_handler' ; import { DrawCanvas } from './event_handler/draw_canvas' ; import { AdornersOptions , AdornersPanelOptions , AdornersPageOptions } from './event_handler/adorners' ; import { SurveyHelper } from './helper_survey' ; /**
 * SurveyPDF object contains options, events and methods to export PDF
 */ export class SurveyPDF extends SurveyModel { private _haveCommercialLicense : boolean ; public options : IDocOptions ; public constructor ( jsonObject , options ? ) { super ( jsonObject ) ; if ( typeof options === 'undefined' ) { options = { } ; } this . options = SurveyHelper . clone ( options ) ; this . _haveCommercialLicense = options . commercial || options . haveCommercialLicense ; } /**
     * You have right to set this property to true if you have bought the commercial licence only.
     * It will remove the text about non-commerical usage on the top of the document.
     * Setting this property true without having a commercial licence is illegal
     */ public get haveCommercialLicense ( ) : boolean { return this . _haveCommercialLicense ; } public set haveCommercialLicense ( val ) { this . _haveCommercialLicense = val ; } param survey param canvas public onRenderHeader : Event < ( survey : SurveyPDF , canvas : DrawCanvas ) => any , any > = new Event < ( survey : SurveyPDF , canvas : DrawCanvas ) => any , any > ( ) ; param survey param canvas public onRenderFooter : Event < ( survey : SurveyPDF , canvas : DrawCanvas ) => any , any > = new Event < ( survey : SurveyPDF , canvas : DrawCanvas ) => any , any > ( ) ; param survey param options public onRenderQuestion : EventAsync < ( survey : SurveyPDF , options : AdornersOptions ) => any , any > = new EventAsync < ( survey : SurveyPDF , options : AdornersOptions ) => any , any > ( ) ; param survey param options public onRenderPanel : EventAsync < ( survey : SurveyPDF , options : AdornersPanelOptions ) => any , any > = new EventAsync < ( survey : SurveyPDF , options : AdornersPanelOptions ) => any , any > ( ) ; param survey param options public onRenderPage : EventAsync < ( survey : SurveyPDF , options : AdornersPageOptions ) => any , any > = new EventAsync < ( survey : SurveyPDF , options : AdornersPageOptions ) => any , any > ( ) ; private waitForQuestionIsReady ( question ) { return new Promise ( ( resolve ) => { if ( question . isReady ) { resolve ( ) ; } else { const readyCallback = ( _ , options ) => { if ( options . isReady ) { question . onReadyChanged . remove ( readyCallback ) ; resolve ( ) ; } } ; question . onReadyChanged . add ( readyCallback ) ; } } ) ; } private async waitForCoreIsReady ( ) { for ( const question of this . getAllQuestions ( ) ) { if ( ! ! ( < any > question ) . contentPanel ) { const list = [ ] ; ( < any > question ) . contentPanel . addQuestionsToList ( list ) ; for ( const innerQuestion of list ) { await this . waitForQuestionIsReady ( innerQuestion ) ; } continue ; } else await this . waitForQuestionIsReady ( SurveyHelper . getContentQuestion ( < Question > question ) ) ; } } private async render ( controller ) { await this . waitForCoreIsReady ( ) ; const flats = await FlatSurvey . generateFlats ( this , controller ) ; const packs = PagePacker . pack ( flats , controller ) ; EventHandler . process_header_events ( this , controller , packs ) ; for ( let i = 0 ; i < packs . length ; i ++ ) { for ( let j = 0 ; j < packs [ i ] . length ; j ++ ) { if ( controller . getNumberOfPages ( ) === i ) { controller . addPage ( ) ; } controller . setPage ( i ) ; await packs [ i ] [ j ] . render ( ) ; } } } param fileName public async save ( fileName = 'survey_result.pdf' ) { const controller = new DocController ( this . options ) ; SurveyHelper . fixFont ( controller ) ; await this . render ( controller ) ; return controller . doc . save ( fileName , { returnPromise : true } ) ; } param type public async raw ( type ? ) { const controller = new DocController ( this . options ) ; SurveyHelper . fixFont ( controller ) ; await this . render ( controller ) ; return controller . doc . output ( type ) ; } }