UI5 | Imprimir un SmartChart a PDF

Extender un Analytical List Page con un botón "Imprimir gráfico a PDF": capturar el SVG del SmartChart, pasarlo a canvas y exportarlo con jsPDF.

Crea y extiende un Analytical List Page (Fiori Elements) para añadir una función de "Imprimir gráfico a PDF" usando UI5 y librerías ligeras en el cliente.

Repositorio de referencia (código completo): https://github.com/GonzaloMB/smartchart_pdf_print_ui5.git

Del SmartChart al PDF, todo en el clienteABC123ON-PREMBROWSERABAP CDS@OData.publish01OData V2serviceALPSmartChart · Print03data URLtoDataURL04canvasdrawImage05chart svgcapturedjsPDFA4 landscape07PDFViewersap.m08capture
FIG. 1Del SmartChart al PDF, todo en el cliente

Requisitos

  • ABAP CDS en un sistema on-premise (OData V2 publicado con @OData.publish: true).
  • SAP BAS o VS Code para el desarrollo FE.
  • SAP Logon / Eclipse (para escribir los ABAP CDS, si aplica).

Backend (ABAP CDS)

  1. Vista básica: consume la tabla sflight del sistema on-premise.
spec · abap
@AbapCatalog.sqlViewName: 'ZISFLIGHTDEMOCDS'
@AbapCatalog.compiler.compareFilter: true
@AbapCatalog.preserveKey: true
@AccessControl.authorizationCheck: #NOT_REQUIRED
@VDM.viewType: #BASIC
@EndUserText.label: 'Basic CDS to capability demo'
define view ZI_SFLIGHT_DEMO_CDS
  as select from sflight
{
key carrid as Carrid,
key connid as Connid,
key fldate as Fldate,
price as Price,
currency as Currency,
planetype as Planetype,
seatsmax as Seatsmax,
seatsocc as Seatsocc,
paymentsum as Paymentsum,
seatsmax_b as SeatsmaxB,
seatsocc_b as SeatsoccB,
seatsmax_f as SeatsmaxF,
seatsocc_f as SeatsoccF

}
  1. Vista de consumo: publica el OData V2.
spec · abap
@AbapCatalog.sqlViewName: 'ZCSFLIGHTCAP'
@AbapCatalog.compiler.compareFilter: true
@AbapCatalog.preserveKey: true
@AccessControl.authorizationCheck: #NOT_REQUIRED
@VDM.viewType: #CONSUMPTION
@Metadata.allowExtensions: true
@OData.publish: true
@EndUserText.label: 'Consumption CDS to capability demo'
define view ZC_SFLIGHT_CAPABILITY_CDS
  as select from ZI_SFLIGHT_DEMO_CDS
{
  key Carrid,
  key Connid,
  key Fldate,
      Price,
      Currency,
      Planetype,
      Seatsmax,
      Seatsocc,
      Paymentsum,
      SeatsmaxB,
      SeatsoccB,
      SeatsmaxF,
      SeatsoccF
}
  1. Metadata extension: separa las anotaciones de la lógica de negocio.
spec · abap
@Metadata.layer: #(sflight)
@UI.chart: [
  {
    chartType: #COLUMN,
    dimensions: [ 'Planetype' ],
    measures: [ 'Seatsmax' ]
  }
]
@UI: {
  headerInfo: {
    typeName: 'Flight',
    typeNamePlural: 'Flights',
    title: { type: #STANDARD, value: 'Carrid' },
    description: { value: 'Planetype' }
  }
}

annotate entity ZC_SFLIGHT_CAPABILITY_CDS with
{
  @UI.facet: [
    { id: 'Fldateheaderid', purpose: #HEADER, type: #DATAPOINT_REFERENCE, position: 10, targetQualifier: 'Fldateheader' },
    { id: 'Currencyheaderid', purpose: #HEADER, type: #DATAPOINT_REFERENCE, position: 20, targetQualifier: 'Priceheader' },
    { label: 'General Information', id: 'GeneralInfo', type: #COLLECTION, position: 10 },
    { id: 'Price', purpose: #STANDARD, type: #IDENTIFICATION_REFERENCE, parentId: 'GeneralInfo', label: 'Price', position: 10 }
  ]
  @UI.lineItem: [{position: 10 }]
  @UI.selectionField: [{position: 10 }]
  Carrid;
  @UI.lineItem: [{position: 20 }]
  @UI.selectionField: [{position: 20 }]
  Connid;
  @UI.lineItem: [{position: 30 }]
  @UI.selectionField: [{position: 30 }]
  @UI.dataPoint: { qualifier: 'Fldateheader', title: 'Flight Date'}
  Fldate;
  @UI.dataPoint: { qualifier: 'Priceheader', title: 'Airfare'}
  @UI.lineItem: [{position: 40 }]
  @UI.identification: [{ position: 10 }]
  Price;
  @UI.dataPoint: { qualifier: 'Currencyheader', title: 'Airline Currency'}
  @UI.lineItem: [{position: 50 }]
  @UI.identification: [{ position: 20 }]
  Currency;
  @UI.lineItem: [{position: 60 }]
  Planetype;
  @UI.lineItem: [{position: 70 }]
  Seatsmax;
  @UI.lineItem: [{position: 80 }]
  Seatsocc;
  @UI.lineItem: [{position: 90 }]
  Paymentsum;
  @UI.lineItem: [{position: 100 }]
  SeatsmaxB;
  @UI.lineItem: [{position: 110 }]
  SeatsoccB;
  @UI.lineItem: [{position: 120 }]
  SeatsmaxF;
  @UI.lineItem: [{position: 130 }]
  SeatsoccF;
}

Frontend (Fiori Elements · Analytical List Page)

  1. Genera una app ALP (plantilla: Analytical List Page). Rellena los datos del proyecto y el servicio OData principal.

  2. Valida los data sources en manifest.json:

spec · json
"dataSources": {
  "mainService": {
    "uri": "/sap/opu/odata/sap/ZC_SFLIGHT_CAPABILITY_CDS_CDS/",
    "type": "OData",
    "settings": {
      "annotations": ["ZC_SFLIGHT_CAPABILITY_CDS_CD_VAN", "annotation"],
      "localUri": "localService/metadata.xml",
      "odataVersion": "2.0"
    }
  },
  "ZC_SFLIGHT_CAPABILITY_CDS_CD_VAN": {
    "uri": "/sap/opu/odata/IWFND/CATALOGSERVICE;v=2/Annotations(TechnicalName='ZC_SFLIGHT_CAPABILITY_CDS_CD_VAN',Version='0001')/$value/",
    "type": "ODataAnnotation",
    "settings": { "localUri": "localService/ZC_SFLIGHT_CAPABILITY_CDS_CD_VAN.xml" }
  },
  "annotation": {
    "type": "ODataAnnotation",
    "uri": "annotations/annotation.xml",
    "settings": { "localUri": "annotations/annotation.xml" }
  }
}
  1. Extensión de controlador: crea webapp/ext/controller/AnalyticalListPageExt.controller.js y referénciala en manifest.json:
spec · json
"extends": {
  "extensions": {
    "sap.ui.controllerExtensions": {
      "sap.suite.ui.generic.template.AnalyticalListPage.view.AnalyticalListPage": {
        "controllerName": "pdfprintui5.ext.controller.AnalyticalListPageExt"
      }
    }
  }
}
  1. Acción personalizada (botón) conectada al método del controlador:
spec · json
"extends": {
  "extensions": {
    "sap.ui.controllerExtensions": {
      "sap.suite.ui.generic.template.AnalyticalListPage.view.AnalyticalListPage": {
        "controllerName": "pdfprintui5.ext.controller.AnalyticalListPageExt",
        "sap.ui.generic.app": {
          "ZC_SFLIGHT_CAPABILITY_CDS": {
            "EntitySet": "ZC_SFLIGHT_CAPABILITY_CDS",
            "Actions": {
              "btnPrintChart": {
                "id": "btnPrintChart",
                "text": "Print Chart to PDF",
                "press": "onPrintChart"
              }
            }
          }
        }
      }
    }
  }
}
  1. Librerías de terceros y CSS opcional en manifest.json (coloca los ficheros en webapp/libs y webapp/css):
spec · json
"resources": {
  "css": [ { "uri": "css/style.css" } ],
  "js": [
    { "uri": "libs/rgbcolor.js" },
    { "uri": "libs/StackBlur.js" },
    { "uri": "libs/canvg.js" },
    { "uri": "libs/jspdf.js" }
  ]
}
  1. Lógica del controlador: captura el SVG del SmartChart, lo pinta en un canvas, lo exporta a PDF con jsPDF y lo muestra con sap.m.PDFViewer.
spec · javascript
sap.ui.define(
  ["sap/ui/core/BusyIndicator", "sap/m/PDFViewer"],
  function (BusyIndicator, PDFViewer) {
    "use strict";
    return sap.ui.controller(
      "pdfprintui5.ext.controller.AnalyticalListPageExt",
      {
        onPrintChart: function () {
          BusyIndicator.show(0);
          this.smartChartId =
            "pdfprintui5::sap.suite.ui.generic.template.AnalyticalListPage.view.AnalyticalListPage::ZC_SFLIGHT_CAPABILITY_CDS--chart";
          this.chart = this.getView()
            .byId(this.smartChartId)
            .getItems()[2]
            .getAggregation("_vizFrame");
          this.chart.setWidth("1100px");
          this.chart.setHeight("700px");
          setTimeout(
            function () {
              this.processChartPDF();
            }.bind(this),
            100
          );
        },
        processChartPDF: function () {
          var svg = this.chart.getDomRef().getElementsByTagName("svg")[0];
          var canvas = document.createElement("canvas");
          var bBox = svg.getBBox();
          canvas.width = bBox.width;
          canvas.height = bBox.height;
          var context = canvas.getContext("2d");
          var imageObj = new Image();
          imageObj.src =
            "data:image/svg+xml," +
            jQuery.sap.encodeURL(
              svg.outerHTML.replace(
                /^<svg/,
                '<svg xmlns="http://www.w3.org/2000/svg" version="1.1"'
              )
            );
          imageObj.onload = function () {
            context.drawImage(imageObj, 0, 0, canvas.width, canvas.height);
            var dataURL = canvas.toDataURL("base64");
            const doc = new jsPDF("l", "px", "a4");
            doc.addImage(dataURL, "jpg", 10, 20);
            this.chart.setWidth("100%");
            this.chart.setHeight("100%");
            var blob = doc.output("blob", "chart_in_pdf.pdf");
            var _pdfurl = URL.createObjectURL(blob);
            this._pdfViewer = new PDFViewer();
            this.getView().addDependent(this._pdfViewer);
            this._PDFViewer = new sap.m.PDFViewer({
              width: "auto",
              source: _pdfurl,
              showDownloadButton: true,
            });
            jQuery.sap.addUrlWhitelist("blob");
            this._PDFViewer.open();
          }.bind(this);
          setTimeout(function () {
            BusyIndicator.hide();
          }, 100);
        },
      }
    );
  }
);

Resolución de problemas

  • URL blob bloqueada: añade jQuery.sap.addUrlWhitelist("blob") antes de abrir el PDFViewer.
  • No se encuentra el gráfico: verifica el id exacto del control SmartChart y la ruta getItems()[2] hasta el _vizFrame interno en tu versión de ALP.
  • PDF vacío: asegúrate de que el SVG se ha renderizado del todo antes de capturarlo (con un retardo) y evita imágenes de otro origen en el SVG (rompen toDataURL).
  • Faltan librerías: comprueba que rgbcolor.js, StackBlur.js, canvg.js y jspdf.js se cargan desde webapp/libs.

Tecnologías

  • ABAP CDS
  • JavaScript / UI5
  • Fiori Elements