Having problems converting into PDF before sending as attachement in email

Posted by: georgeg on 4 December 2025, 2:31 pm EST

  • Posted 4 December 2025, 2:31 pm EST

    Hi,

    Encountered some issues when trying to tink with the format of the activeSheet as PDF I will eventually package for

    email to my .NET Core Minimal API.

    Here is my test code:

    function sendActiveSheetAsPdf() {
        const recipientEmail  = $("#email-to").val();
        const optionalMessage = $("#email-message").val(); // note the # to select by id
        if (!recipientEmail) {
            alert("Please enter a recipient email.");
            return;
        }
    
        // Find Designer and get SpreadJS workbook
        const designer = GC.Spread.Sheets.Designer.findControl(document.getElementById("designerHost"));
        const spread = designer.getWorkbook();
        const activeIndex = spread.getActiveSheetIndex(); // we need the index for savePDF
    
        // (Optional) tweak print settings for better PDF layout on the active sheet
        const sheet = spread.getActiveSheet();
        const pi = sheet.printInfo();
        pi.orientation(GC.Spread.Sheets.Print.PrintOrientation.landscape); // or portrait
        pi.paperSize(new GC.Spread.Sheets.Print.PaperSize("letter"));      // A4, Letter, etc.
        pi.fitPagesWide(1);                                                // fit to 1 page wide
        pi.fitPagesTall(0);                                                // unlimited height
        sheet.printInfo(pi);
    
        // Export active sheet to PDF. Aledgedly this will work.
        spread.savePDF(
            function (blob) {
                const formData = new FormData();
                formData.append("file", blob, "ActiveSheet.pdf");
                formData.append("recipientEmail", recipientEmail);
                formData.append("optionalMessage", optionalMessage || "");
                console.log(" FORM DATA:"+ formData)
    
                // THIS IS DUMMY CODE ... ignore.
                //Insert endpoint api here and form Data.
                // fetch("/api/email/send-sheet", {
                //     method: "POST",
                //     body: formData
                // })
                // .then(r => {
                //     //Per current convention this is a 201 Status but the ok is looking for 2XX 
                //     // we can get precise return val with r.status.
                //     if (!r.ok) throw new Error(`HTTP ${r.status}`);
                //     console.log("PDF emailed successfully. STATUS: "+ r.status);
                // })
                // .catch(err => console.error("Error:", err));
    
                $("#email-pop-up-window").data("kendoWindow").close();
            },
            function (error) {
                console.error("PDF export error:", error);
                alert("Failed to export the sheet to PDF.");
            },
            {
                // Only export the active sheet, not the whole workbook
                sheetIndex: activeIndex,
    
                // (Optional) PDF metadata
                title: "Active Sheet",
                author: "SpreadJS",
                subject: "Exported from Designer",
                keywords: "SpreadJS, PDF"
            }
        );
    }

    I am getting an error thrown:

    Uncaught TypeError: Cannot read properties of undefined (reading ‘landscape’)

    at sendActiveSheetAsPdf (emailbutton.js:200:60)

    at init.click (index.html:465:21)

    at init.trigger (kendo.custom.min.js:5:4952)

    at init._click (kendo.custom.min.js:29:1644)

    at HTMLButtonElement.dispatch (jquery-3.4.1.min.js:2:42571)

    at v.handle (jquery-3.4.1.min.js:2:40572)

    I am guessing this code is wrong or invalid:

      pi.orientation(GC.Spread.Sheets.Print.PrintOrientation.landscape);

    The order of my SpreadJS java script files on my index.html is:

       <!--CSS files-->
        <link rel="stylesheet" href="css/default-ocean-blue.css" type="text/css" />
        <link href="css/gc.spread.sheets.18.0.0.css" rel="stylesheet" type="text/css" />
        <link href="css/gc.spread.sheets.designer.18.0.0.min.css" rel="stylesheet" type="text/css" />
        <link href="css/default-ocean-blue.css" rel="stylesheet" type="text/css" />
        <link href="FontAwesome/font-awesome-4.7.0/css/font-awesome.min.css" type="text/css" />
        <link href="css/site.css" rel="stylesheet" type="text/css" />
        <!-- KENDO STUFF -->
        <!-- <script src="scripts/jszip.min.js" type="text/javascript"></script> -->
        <!--KENDO STUFF:-->
        <script src="jQuery/jquery-3.4.1.min.js" type="text/javascript"></script>
        <script src="scripts/jszip.min.js" type="text/javascript"></script>
        <script src="Kendo/kendo.custom.min.js" type="text/javascript"></script>
        <script src="./kendo-ui-license.js" type="text/javascript"></script>
    
        <!--Spread JS Script files-->
        <script src="scripts/gc.spread.sheets.all.18.0.0.min.js" type="text/javascript"></script>
        <script src="scripts/gc.spread.sheets.shapes.18.0.0.min.js" type="text/javascript"></script>
        <script src="scripts/gc.spread.sheets.charts.18.0.0.min.js" type="text/javascript"></script>
    
        <script src="scripts/gc.spread.sheets.io.18.0.0.min.js" type="text/javascript"></script>
        <script src="scripts/gc.spread.sheets.print.18.0.0.min.js" type="text/javascript"></script>
        <script src="scripts/gc.spread.sheets.pdf.18.0.0.min.js" type="text/javascript"></script>
        <!-- <script src="scripts/gc.spread.excelio.18.0.0.min.js" type="text/javascript"></script> -->
    
        <!--SpreadJS Designer Script files-->
        <script src="scripts/gc.spread.sheets.designer.resource.en.18.0.0.min.js"></script>
        <script src="scripts/gc.spread.sheets.designer.all.18.0.0.min.js"></script>

    Seems like when I comment the code out that does the settings the PDF conversion works fine (I am not seeing any errors or exceptions thrown).

    Thanks!

    George

  • Posted 4 December 2025, 4:57 pm EST

    Hi,

    Just a continuation of the previous post.

    I found out that the arguement in the orientation() function is:

    pi.orientation(GC.Spread.Sheets.Print.PrintPageOrientation.landscape);

    and NOT:

    pi.orientation(GC.Spread.Sheets.Print.PrintOrientation.landscape);

    Other concern is do I use the PrintInfo() object that is part of the activeSheet or a new one? Ex:

        // (Optional) tweak print settings for better PDF layout on the active sheet
        const sheet = spread.getActiveSheet();
    
       // PrintInfo object currently attached to the activeSheet:
        const pi = sheet.printInfo();
    
        // Is this correct? The API documentation shows this; a new PrintInfo() object and not the current 
        //     PrintInfo() object(???)
        //var pi = new GC.Spread.Sheets.Print.PrintInfo(); //might be this and not the above two lines.
        pi.bestFitColumns(true);
        pi.orientation(GC.Spread.Sheets.Print.PrintPageOrientation.landscape);
    
    
        //pi.orientation(GC.Spread.Sheets.Print.PrintOrientation.landscape); // or portrait
        pi.paperSize(new GC.Spread.Sheets.Print.PaperSize("letter"));      // A4, Letter, etc.
        pi.fitPagesWide(1);                                                // fit to 1 page wide
        pi.fitPagesTall(0);                                                // unlimited height
        sheet.printInfo(pi);
    
  • Posted 5 December 2025, 2:27 am EST

    Hi George,

    Thanks for your questions.

    You’re right about the enum - the correct one is:

    GC.Spread.Sheets.Print.PrintPageOrientation.landscape

    PrintOrientation is not part of the SpreadJS print module, which is why the earlier call failed.

    Regarding which PrintInfo instance to use:

    SpreadJS supports both patterns; however, updating the existing printInfo on the sheet is the recommended approach since it already contains the required defaults and applied settings. When you modify this instance, the changes are applied in place, so there is no need to reassign it back to the sheet.

    Additionally, there is a small issue in your current code when setting the paper size. Passing “letter” as a string will result in a corrupted PDF. Instead, use the strongly-typed enum:

    GC.Spread.Sheets.Print.PaperKind.letter

    This ensures the PDF is generated correctly.

    You can also refer to the attached sample demonstrating the working PDF export setup.

    Please let us know if you encounter any further issues.

    Kind Regards,

    Chirag

    Attachment: https://jscodemine.mescius.io/share/J3PIsPo5MEKIuLda3I1zNQ

    References:

    1. SpreadJS Print API: https://developer.mescius.com/spreadjs/api/modules/GC.Spread.Sheets.Print
    2. Demos | PDF Export: https://developer.mescius.com/spreadjs/demos/features/pdf/basic-pdf/purejs
    3. Docs | Print: https://developer.mescius.com/spreadjs/docs/features/printing
Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels