UNPKG

@ckeditor/ckeditor5-export-pdf

Version:

Export to PDF feature for CKEditor 5.

23 lines • 9.8 kB
/* * Copyright (c) 2003-2026, CKSource Holding sp. z o.o. All rights reserved. * * * * * +---------------------------------------------------------------------------------+ * | | * | Hello stranger! | * | | * | | * | What you're currently looking at is the source code of a legally protected, | * | proprietary software. Any attempts to deobfuscate / disassemble this code | * | are forbidden and will result in legal consequences. | * | | * | | * +---------------------------------------------------------------------------------+ * * * * */ import{Command as _0x3100cf,Plugin as _0x2f739f}from'@ckeditor/ckeditor5-core/dist/index.js';import{collectStylesheets as _0x3c917e}from'@ckeditor/ckeditor5-utils/dist/index.js';import{ButtonView as _0x4ed830,MenuBarMenuListItemButtonView as _0x8f0cfa,SpinnerView as _0x49fa3c,Notification as _0x13f794}from'@ckeditor/ckeditor5-ui/dist/index.js';import{CloudServices as _0x100d04}from'@ckeditor/ckeditor5-cloud-services/dist/index.js';import{IconExportPdf as _0x2147bf}from'@ckeditor/ckeditor5-icons/dist/index.js';function u(_0x324faf,_0x3d85a0){const t=_0x324faf['t'];switch(_0x3d85a0){case'Export\x20to\x20PDF':return t('Export\x20to\x20PDF');case'An\x20error\x20occurred\x20while\x20generating\x20the\x20PDF.':return t('An\x20error\x20occurred\x20while\x20generating\x20the\x20PDF.');case'PDF\x20export\x20started':return t('PDF\x20export\x20started');case'PDF\x20export\x20failed':return t('PDF\x20export\x20failed');case'PDF\x20export\x20successful':return t('PDF\x20export\x20successful');case'Exporting\x20PDF\x20document':return t('Exporting\x20PDF\x20document');default:return _0x3d85a0;}}class ExportPdfCommand extends _0x3100cf{constructor(_0x1d5cd4){super(_0x1d5cd4),this['set']('isBusy',!0x1),this['affectsData']=!0x1;}['refresh'](){this['isEnabled']=!this['isBusy'],this['value']=this['isBusy']?'pending':void 0x0;}['execute'](_0x50e3ba={}){const {editor:_0x1d142d}=this,_0x16a173=_0x50e3ba['version']||_0x1d142d['config']['get']('exportPdf.version'),_0x2c25d0=_0x50e3ba['converterUrl']||function(_0x283d11){return 0x2===_0x283d11?'https://pdf-converter.cke-cs.com/v2/convert/html-pdf':'https://pdf-converter.cke-cs.com/v1/convert/';}(_0x16a173),_0x1f0b15=_0x50e3ba['dataCallback']||(_0x52ad4d=>_0x52ad4d['data']['get']()),_0x27a8d3=_0x1d142d['plugins']['get']('ExportPdf'),_0x4ce648=_0x50e3ba['token']||_0x27a8d3['_token'];return this['isBusy']=!0x0,this['refresh'](),_0x3c917e(_0x50e3ba['stylesheets']||['EDITOR_STYLES'])['then'](async _0x437b25=>{if(_0x1d142d['ui']['ariaLiveAnnouncer']['announce'](u(_0x1d142d['locale'],'PDF\x20export\x20started')),_0x1d142d['plugins']['has']('MergeFields')){_0x50e3ba['converterOptions']||(_0x50e3ba['converterOptions']={});const _0x2c6d31=_0x1d142d['plugins']['get']('MergeFieldsEditing'),_0x89d2d8=_0x2c6d31['previewMode'],{prefix:_0xad939f,suffix:_0x325c73}=_0x1d142d['config']['get']('mergeFields'),_0x5714bb='data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP88B8AAuUB8e2ujYwAAAAASUVORK5CYII=',_0x7ac776={'prefix':_0xad939f,'suffix':_0x325c73,'data':{}};if('$labels'===_0x89d2d8){for(const _0x500fe8 of _0x2c6d31['getMergeFieldsIds']())'image'===_0x2c6d31['getMergeFieldType'](_0x500fe8)?_0x7ac776['data'][_0x500fe8]=_0x5714bb:_0x7ac776['data'][_0x500fe8]=_0x2c6d31['wrapInAffixes'](_0x2c6d31['getLabel'](_0x500fe8)||_0x500fe8);}else{const _0x15560b='$defaultValues'===_0x89d2d8?_0x2c6d31['getDefaultValues']():_0x2c6d31['getDataSetValues'](_0x89d2d8,!0x0);for(const _0x599d27 of Object['entries'](_0x15560b)){const [_0xe68fa,_0x13cc5f]=_0x599d27;'image'===_0x2c6d31['getMergeFieldType'](_0xe68fa)?_0x7ac776['data'][_0xe68fa]=null!==_0x13cc5f?_0x13cc5f:_0x5714bb:_0x7ac776['data'][_0xe68fa]=null!==_0x13cc5f?_0x13cc5f:_0x2c6d31['getLabel'](_0xe68fa)||_0xe68fa;}}_0x50e3ba['converterOptions']['merge_fields']=_0x7ac776;}const _0x2c79c2=0x2===_0x16a173?function(_0x451976){const _0x297365=_0x451976?.['document']?.['margins'];return f({'pageWidth':m(_0x451976?.['document']?.['size']||'A4'),'marginLeft':_0x297365?.['left'],'marginRight':_0x297365?.['right']});}(_0x50e3ba['converterOptions']):(_0x132550=_0x50e3ba['converterOptions'],f({'pageWidth':m(_0x132550?.['format']||'A4'),'marginLeft':_0x132550?.['margin_left'],'marginRight':_0x132550?.['margin_right']}));var _0x132550;const _0xadea7a={'html':('\x0a\x09\x09\x09\x09\x09<!DOCTYPE\x20html>\x0a\x09\x09\x09\x09\x09<html>\x0a\x09\x09\x09\x09\x09\x09<head>\x0a\x09\x09\x09\x09\x09\x09\x09<title>CKEditor\x205\x20PDF\x20document</title>\x0a\x09\x09\x09\x09\x09\x09\x09<meta\x20charset=\x22utf-8\x22>\x0a\x09\x09\x09\x09\x09\x09</head>\x0a\x09\x09\x09\x09\x09\x09<body>\x0a\x09\x09\x09\x09\x09\x09\x09<div\x20class=\x22ck-content\x22\x20dir=\x22'+_0x1d142d['locale']['contentLanguageDirection']+'\x22\x20style=\x22'+_0x2c79c2+'\x22>\x0a\x09\x09\x09\x09\x09\x09\x09\x09'+await _0x1f0b15(_0x1d142d)+'\x0a\x09\x09\x09\x09\x09\x09\x09</div>\x0a\x09\x09\x09\x09\x09\x09</body>\x0a\x09\x09\x09\x09\x09</html>\x0a\x09\x09\x09\x09')['trim'](),'css':_0x437b25,[0x2===_0x16a173?'config':'options']:_0x50e3ba['converterOptions']},_0x8f5ebd={'method':'POST','headers':{'Content-Type':'application/json','Accept':'application/pdf',..._0x4ce648?{'Authorization':_0x4ce648['value']}:null,'x-cs-app-id':_0x50e3ba['appID']||'cke5'},'body':JSON['stringify'](_0xadea7a)};return window['fetch'](_0x2c25d0,_0x8f5ebd)['then'](_0xf2ffdc=>{if(0xc8!==_0xf2ffdc['status'])throw _0xf2ffdc;return _0xf2ffdc['blob']();})['then'](_0x307616=>{_0x1d142d['ui']['ariaLiveAnnouncer']['announce'](u(_0x1d142d['locale'],'PDF\x20export\x20successful'));const _0x509149='function'==typeof _0x50e3ba['fileName']?_0x50e3ba['fileName']():_0x50e3ba['fileName'];this['_downloadFile'](_0x307616,_0x509149||'document.pdf');});})['catch'](_0x286db2=>{const _0x5e6f47=_0x1d142d['plugins']['get']('Notification');throw _0x1d142d['ui']['ariaLiveAnnouncer']['announce'](u(_0x1d142d['locale'],'PDF\x20export\x20failed'),{'politeness':'assertive'}),_0x5e6f47['showWarning'](u(_0x1d142d['locale'],'An\x20error\x20occurred\x20while\x20generating\x20the\x20PDF.')),_0x286db2;})['finally'](()=>{this['isBusy']=!0x1,this['refresh']();});}['_downloadFile'](_0x347a3a,_0x3986fb){const _0x3d1d76=document['createElement']('a');_0x3d1d76['href']=window['URL']['createObjectURL'](_0x347a3a),_0x3d1d76['download']=_0x3986fb,_0x3d1d76['click'](),_0x3d1d76['remove']();}}function f({pageWidth:_0x3c3608,marginLeft:_0x5aaf21,marginRight:_0x4913a1}){return'width:calc('+[_0x3c3608,_0x5aaf21,_0x4913a1]['filter'](Boolean)['join']('\x20-\x20')+');';}function m(_0x4d3176){if('object'==typeof _0x4d3176&&'width'in _0x4d3176)return _0x4d3176['width'];return{'letter':['21.6cm','27.9cm'],'legal':['21.6cm','35.6cm'],'tabloid':['27.9cm','43.2cm'],'ledger':['43.2cm','27.9cm'],'a0':['84.1cm','118.9cm'],'a1':['59.4cm','84.1cm'],'a2':['42.0cm','59.4cm'],'a3':['29.7cm','42.0cm'],'a4':['21.0cm','29.7cm'],'a5':['14.8cm','21.0cm'],'a6':['10.5cm','14.8cm']}[_0x4d3176['toLowerCase']()][0x0];}class ExportPdfUI extends _0x2f739f{static get['pluginName'](){return'ExportPdfUI';}static get['isOfficialPlugin'](){return!0x0;}static get['isPremiumPlugin'](){return!0x0;}['init'](){const _0x4b4903=this['editor'];_0x4b4903['ui']['componentFactory']['add']('exportPdf',()=>{const _0x2d2c6a=this['_createButton'](_0x4ed830),_0x587be2=_0x4b4903['commands']['get']('exportPdf');return _0x2d2c6a['set']({'tooltip':!0x0}),_0x2d2c6a['bind']('isOn')['to'](_0x587be2,'isBusy'),_0x2d2c6a;}),_0x4b4903['ui']['componentFactory']['add']('menuBar:exportPdf',()=>this['_createButton'](_0x8f0cfa));}['_createButton'](_0x5c517a){const _0x526938=this['editor'],_0x38095c=_0x526938['config']['get']('exportPdf'),_0x2f7650=_0x526938['commands']['get']('exportPdf'),_0x31c1c9=new _0x5c517a(_0x526938['locale']),_0x140f4e=new _0x49fa3c();return _0x31c1c9['children']['add'](_0x140f4e),_0x140f4e['bind']('isVisible')['to'](_0x2f7650,'isBusy'),_0x31c1c9['iconView']['bind']('isVisible')['to'](_0x2f7650,'isBusy',_0x3375c3=>!_0x3375c3),_0x31c1c9['set']({'icon':_0x2147bf}),_0x31c1c9['bind']('isEnabled')['to'](_0x2f7650,'isEnabled'),_0x31c1c9['bind']('label')['to'](_0x2f7650,'isBusy',_0x47c2d8=>u(_0x526938['locale'],_0x47c2d8?'Exporting\x20PDF\x20document':'Export\x20to\x20PDF')),this['listenTo'](_0x31c1c9,'execute',()=>{_0x526938['execute']('exportPdf',_0x38095c),_0x526938['editing']['view']['focus']();}),_0x31c1c9;}}class ExportPdf extends _0x2f739f{['_token'];static get['pluginName'](){return'ExportPdf';}static get['isOfficialPlugin'](){return!0x0;}static get['isPremiumPlugin'](){return!0x0;}static get['requires'](){return[_0x100d04,_0x13f794,ExportPdfUI];}['init'](){const {editor:_0x274052}=this;_0x274052['config']['define']('exportPdf.version',0x2),_0x274052['commands']['add']('exportPdf',new ExportPdfCommand(_0x274052));const _0x57346c=_0x274052['plugins']['get'](_0x100d04),_0x31d6db=_0x274052['config']['get']('exportPdf');!0x1===_0x31d6db['tokenUrl']?this['_token']=null:_0x31d6db['tokenUrl']?_0x57346c['registerTokenUrl'](_0x31d6db['tokenUrl'])['then'](_0x420132=>{this['_token']=_0x420132;}):this['_token']=_0x57346c['token'];}}export{ExportPdf,ExportPdfCommand};