h17-sspdf 0.1.1 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -0
- package/core/pdf-core.js +1 -1
- package/core/plugin-chart.js +40 -23
- package/package.json +1 -1
package/README.md
CHANGED
package/core/pdf-core.js
CHANGED
|
@@ -130,7 +130,7 @@ class PDFCore {
|
|
|
130
130
|
subject: meta.subject || "",
|
|
131
131
|
author: meta.author || "Hugo Palma",
|
|
132
132
|
keywords: meta.keywords || "",
|
|
133
|
-
creator: "SuperSimplePDF (github.com/hugopalma17/sspdf)",
|
|
133
|
+
creator: "SuperSimplePDF (github.com/hugopalma17/sspdf) — built on jsPDF",
|
|
134
134
|
});
|
|
135
135
|
|
|
136
136
|
this.paintBackground();
|
package/core/plugin-chart.js
CHANGED
|
@@ -64,34 +64,51 @@ function getCanvas() {
|
|
|
64
64
|
return _ChartJSNodeCanvas;
|
|
65
65
|
}
|
|
66
66
|
|
|
67
|
+
/**
|
|
68
|
+
* Pre-render the chart to a PNG buffer and cache it on the operation.
|
|
69
|
+
* Call this before renderDocument() for any source containing chart operations.
|
|
70
|
+
* @param {object} operation - the chart operation object (mutated in place)
|
|
71
|
+
* @returns {Promise<void>}
|
|
72
|
+
*/
|
|
73
|
+
async function preRender(operation) {
|
|
74
|
+
const ChartJSNodeCanvas = getCanvas();
|
|
75
|
+
const canvasW = operation.canvasWidth || 1600;
|
|
76
|
+
const canvasH = operation.canvasHeight || 800;
|
|
77
|
+
|
|
78
|
+
const canvas = new ChartJSNodeCanvas({
|
|
79
|
+
width: canvasW,
|
|
80
|
+
height: canvasH,
|
|
81
|
+
backgroundColour: 'transparent',
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
operation._buf = await canvas.renderToBuffer({
|
|
85
|
+
type: operation.chartType || 'bar',
|
|
86
|
+
data: operation.data || { labels: [], datasets: [] },
|
|
87
|
+
options: {
|
|
88
|
+
...(operation.options || {}),
|
|
89
|
+
responsive: false,
|
|
90
|
+
animation: false,
|
|
91
|
+
},
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
|
|
67
95
|
module.exports = {
|
|
68
|
-
|
|
69
|
-
const { core, operation, bounds } = ctx;
|
|
70
|
-
const ChartJSNodeCanvas = getCanvas();
|
|
96
|
+
preRender,
|
|
71
97
|
|
|
72
|
-
|
|
73
|
-
const
|
|
74
|
-
const canvasW = operation.canvasWidth || 1600;
|
|
75
|
-
const canvasH = operation.canvasHeight || 800;
|
|
76
|
-
const x = operation.xMm !== undefined ? operation.xMm : bounds.left;
|
|
98
|
+
render(ctx) {
|
|
99
|
+
const { core, operation, bounds } = ctx;
|
|
77
100
|
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
}
|
|
101
|
+
if (!operation._buf) {
|
|
102
|
+
throw new Error(
|
|
103
|
+
'chart plugin: operation._buf is missing — call plugin.preRender(operation) before renderDocument()'
|
|
104
|
+
);
|
|
105
|
+
}
|
|
83
106
|
|
|
84
|
-
const
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
options: {
|
|
88
|
-
...(operation.options || {}),
|
|
89
|
-
responsive: false,
|
|
90
|
-
animation: false,
|
|
91
|
-
},
|
|
92
|
-
});
|
|
107
|
+
const widthMm = operation.widthMm || (bounds.right - bounds.left);
|
|
108
|
+
const heightMm = operation.heightMm || 80;
|
|
109
|
+
const x = operation.xMm !== undefined ? operation.xMm : bounds.left;
|
|
93
110
|
|
|
94
|
-
core.drawImage({ data:
|
|
111
|
+
core.drawImage({ data: operation._buf, format: 'PNG', x, widthMm, heightMm });
|
|
95
112
|
},
|
|
96
113
|
|
|
97
114
|
estimateHeight(ctx) {
|