From 2b03529a4c9527bfd235a1b68836598e6e87c11d Mon Sep 17 00:00:00 2001 From: Christian Georgi Date: Tue, 14 Mar 2023 15:28:26 +0100 Subject: [PATCH] PDF export PoC --- fiori/app/browse/fiori-service.cds | 10 ++++++++ fiori/package.json | 10 ++++++++ fiori/pdfme.js | 38 ++++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+) create mode 100644 fiori/pdfme.js diff --git a/fiori/app/browse/fiori-service.cds b/fiori/app/browse/fiori-service.cds index 03ff198c..f2730993 100644 --- a/fiori/app/browse/fiori-service.cds +++ b/fiori/app/browse/fiori-service.cds @@ -1,5 +1,15 @@ using CatalogService from '@capire/bookstore'; +using from '@sap/cds-pdf-export'; + +annotate CatalogService with @( + Capabilities: { SupportedFormats : [ 'application/pdf' ] }, + PDF.Features: { + DocumentDescriptionReference : '/-pdf/', + DocumentDescriptionCollection : 'createDocumentDescription' + } +); + //////////////////////////////////////////////////////////////////////////// // // Books Object Page diff --git a/fiori/package.json b/fiori/package.json index 187e4059..8e3c530b 100644 --- a/fiori/package.json +++ b/fiori/package.json @@ -43,6 +43,16 @@ "[production]": { "model": "db/hana" } + }, + "pdf": { + "kind": "btp-adobe-forms", + "vcap": { + "label": "adsrestapi" + }, + "[pdfme]": { + "kind": "export-pdf", + "impl": "./pdfme.js" + } } }, "hana": { diff --git a/fiori/pdfme.js b/fiori/pdfme.js new file mode 100644 index 00000000..e54024f5 --- /dev/null +++ b/fiori/pdfme.js @@ -0,0 +1,38 @@ +const { generate, BLANK_PDF } = require("@pdfme/generator"); + +/** + * Generate PDF with @pdfme/generator library + */ +module.exports = async (data, headers) => { + + let inputs = data + + let x = 0, y = 0; + const width = 30; + const height = 5; + + const tableSchema = {} + for (const entry of headers) { + x += width; + tableSchema[entry.Name] = { + type: 'text', + position: { x, y: 10 }, + width, + height + } + } + + for (const row of data) { + for (const [key, value] of Object.entries(row)) { + if (typeof value !== 'string') row[key] = ''+value // stringify + } + } + + const template = { + basePdf: BLANK_PDF, + schemas: [tableSchema] + }; + const pdf = await generate({ template, inputs }); + + return pdf; +};