Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Background image setup #3

Merged
merged 3 commits into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}