export to pdf - unable to find PdfDocument

Hi,


In this example - https://ej2.syncfusion.com/javascript/documentation/document-editor/how-to/export-document-as-pdf, to export to PDF on the clientside, code is provided to use PdfDocument but thats in the typescript. What is the namespace to use for accessing PdfDocument in the vanilla javascript. I get error when I use the below code in javascript - Uncaught TypeError: Cannot read properties of undefined (reading 'PdfDocument')

let pdfdocument = new PdfDocument();

Thanks

Sri


3 Replies

BS Balamurugan Shanmugam Syncfusion Team March 11, 2024 09:56 AM UTC

Hi Sri,

Please find below a code snippet for exporting the pdf document on vanilla JavaScript.

Code snippet:

let pdfdocument = new ej.pdfexport.PdfDocument();

    let count = container.documentEditor.pageCount;

    container.documentEditor.documentEditorSettings.printDevicePixelRatio = 2;

    let loadedPage = 0;

    for (let i = 1i <= counti++) {

        setTimeout(() => {

            let format = 'image/jpeg';

            // Getting pages as image

            let image = container.documentEditor.exportAsImage(iformat);

            image.onload = function () {

                let imageHeight = parseInt(

                    image.style.height.toString().replace('px''')

                );

                let imageWidth = parseInt(

                    image.style.width.toString().replace('px''')

                );

                let section = pdfdocument.sections.add();

                let settings = new ej.pdfexport.PdfPageSettings(0);

                if (imageWidth > imageHeight) {

                    settings.orientation = PdfPageOrientation.Landscape;

                }

                settings.size = new ej.pdfexport.SizeF(imageWidthimageHeight);

                section.setPageSettings(settings);

                let page = section.pages.add();

                let graphics = page.graphics;

                let imageStr = image.src.replace('data:image/jpeg;base64,''');

                let pdfImage = new ej.pdfexport.PdfBitmap(imageStr);

                graphics.drawImage(pdfImage00imageWidthimageHeight);

                loadedPage++;

                if (loadedPage == count) {

                    // Exporting the document as pdf

                    pdfdocument.save(

                        (container.documentEditor.documentName === ''

                            ? 'sample'

                            : container.documentEditor.documentName) + '.pdf'

                    );

                }

            };

        }, 500);

    }

 

Regards,

Balamurugan S




SN Sri Nistala replied to Balamurugan Shanmugam March 11, 2024 12:46 PM UTC

Thanks Bala, I'm still receiving the same error. I did include the ej2-pdf-export.min.js file as well in the html scripts. are there any other dependencies? 


 filename: ej2-pdf-export.min.js

 version : 24.1.41

 filename: ej2-pdf-export.min.js
*  version : 24.1.41


BS Balamurugan Shanmugam Syncfusion Team March 12, 2024 09:45 AM UTC

Hi Sri,


Based on the information, we attempted to recreate the issue by using the below-attached sample. But we didn't encounter any issues. PDF export is working properly. 

Please find below an attached sample for your reference.

index1.html—in this file, we refer to an individual component in a cdn script.

index2.html—in this file, we refer to ej2.min in a cdn script.


Regards,

Balamurugan S


Attachment: New_folder_(6)_2a0a2fa7.zip

Loader.
Up arrow icon