Custom PDF Generation - Landscape

Andrew_TND
Mega Sage
Mega Sage

Hello, I've created a script include and snippet from the widget which pulls through the data on a service portal page.

This works all well and good but I cant for the life of me get it to generate in Landscape not portrait? Any ideas welcome. 😁

//SP SNIPPET

const ga = new GlideAjax("PDFExportHelper");
    ga.addParam("sysparm_name", "generatePDF");
    ga.addParam("sysparm_html", html);
    ga.addParam("sysparm_project", $scope.c.selectedProject);
    ga.addParam("sysparm_date", $scope.c.selectedDate);

    const selectedRecord = $scope.c.filteredData?.[$scope.c.selectedProject]?.[0];
    if (!selectedRecord || !selectedRecord.sys_id) {
      console.warn("Missing selected record or sys_id.");
      return;
    }

    ga.addParam("sysparm_record_id", selectedRecord.sys_id);
    console.log("Calling server with record ID:", selectedRecord.sys_id);

    ga.getXMLAnswer(function (recordSysId) {
      console.log("Returned sys_id:", recordSysId);
      if (recordSysId) {
        $scope.$apply(() => {
          $scope.c.pdfGenerated = true;
        });
      } else {
        console.warn("No sys_id returned");
      }
    });
  };



// Script includes

var PDFExportHelper = Class.create();
PDFExportHelper.prototype = Object.extendsObject(AbstractAjaxProcessor, {
  generatePDF: function () {
    try {
      var html = this.getParameter("sysparm_html");
      var project = this.getParameter("sysparm_project");
      var date = this.getParameter("sysparm_date");
      var recordId = this.getParameter("sysparm_record_id");
      if (!recordId) return '';

      var today = new GlideDateTime();
      var gd = today.getDate();
      var ukToday = today.getDayOfMonthLocalTime().toString().padStart(2, '0') + '-' +
                    (today.getMonthLocalTime()).toString().padStart(2, '0') + '-' +
                    today.getYearLocalTime();

      var parts = date.split('-');
      var asOnUK = (parts.length === 3) ? parts[2] + '-' + parts[1] + '-' + parts[0] : 'unknown';

      var pdfName = ukToday + ' - ' + asOnUK + '.pdf';

      var api = new sn_pdfgeneratorutils.PDFGenerationAPI();
      var config = { orientation: 'landscape' };

      var attachedToSysId = api.convertToPDF(
        html,
        "project_status",
        recordId,
        pdfName,
        "",
        config
      );

      if (!attachedToSysId) return '';

      return recordId;
    } catch (e) {
      return '';
    }
  }
});

 

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@Andrew_TND 

try this

PDFGenerationAPI - Scoped, Global 

      var api = new sn_pdfgeneratorutils.PDFGenerationAPI();

      var hfInfo = new Object();
      hfInfo["PageOrientation"] = "landscape";

      var attachedToSysId = api.convertToPDFWithHeaderFooter(
          html,
          "project_status",
          recordId,
          pdfName,
          hfInfo
      );

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

2 REPLIES 2

Ankur Bawiskar
Tera Patron
Tera Patron

@Andrew_TND 

try this

PDFGenerationAPI - Scoped, Global 

      var api = new sn_pdfgeneratorutils.PDFGenerationAPI();

      var hfInfo = new Object();
      hfInfo["PageOrientation"] = "landscape";

      var attachedToSysId = api.convertToPDFWithHeaderFooter(
          html,
          "project_status",
          recordId,
          pdfName,
          hfInfo
      );

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@Ankur Bawiskar that did the trick! Thanks again!