Skip to content

Commit

Permalink
Merge pull request #3 from somaromero/background-image-setup
Browse files Browse the repository at this point in the history
Background image setup
  • Loading branch information
somaromero authored Sep 13, 2024
2 parents b8056f8 + 8acc931 commit 29a4a4a
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 35 deletions.
2 changes: 1 addition & 1 deletion dist/api.js

Large diffs are not rendered by default.

66 changes: 36 additions & 30 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "directus-extension-pdf-builder",
"description": "This package is a node for Directus flows, utilizing the PDFMake library for generating PDF documents.",
"icon": "extension",
"version": "1.1.0",
"version": "1.2.0",
"keywords": [
"directus",
"directus-extension",
Expand All @@ -24,7 +24,7 @@
"app": "src/app.js",
"api": "src/api.js"
},
"host": "^10.13.2"
"host": "^11.1.0"
},
"scripts": {
"build": "directus-extension build",
Expand Down
30 changes: 28 additions & 2 deletions src/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@ export default {
pdfMake.vfs = await getBase64Fonts(fonts, assetsService);
pdfMake.fonts = getPdfMakeFonts(fonts);

template['images'] = await addImages(images, assetsService, filesService);
const pdfDoc = convertStringToStructure(template);
pdfDoc['images'] = await addImages(images, assetsService, filesService);

const pdfDocGenerator = pdfMake.createPdf(template);
const pdfDocGenerator = pdfMake.createPdf(pdfDoc);

const buffer = await new Promise((resolve, reject) => {
pdfDocGenerator.getBuffer((buffer) => {
Expand Down Expand Up @@ -233,3 +234,28 @@ async function addImages(images, assetsService, filesService) {

return imageList;
}

function convertStringToStructure(template) {
const regexArray = /^\[(.*?)]$/;
const regexObject = /^\{(.*?)}$/;

if (typeof template === 'string' && regexArray.test(template)) {
return JSON.parse(template);
}

if (typeof template === 'string' && regexObject.test(template)) {
return JSON.parse(template);
}

if (Array.isArray(template)) {
return template.map(item => convertStringToStructure(item));
}

if (typeof template === 'object' && template !== null) {
for (let key in template) {
template[key] = convertStringToStructure(template[key]);
}
}

return template;
}

0 comments on commit 29a4a4a

Please sign in to comment.