Skip to content

Commit 48b50bf

Browse files
authored
feat: add support for transparent groups, ensure endGroup would merge sub-canvas text/line/etc. back to primary output data. this completes the fix for #418 (#420)
1 parent de176e5 commit 48b50bf

File tree

3 files changed

+31
-5
lines changed

3 files changed

+31
-5
lines changed

lib/pdf.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,8 @@ export default class PDFJSClass extends EventEmitter {
359359
const scaledSpaceWidth = spaceWidth * textHScale;
360360

361361
// Add spaces if gap is positive and significant (> 30% of scaled space width)
362-
if (gap > scaledSpaceWidth * 0.3) {
362+
// Also check that scaledSpaceWidth is valid to avoid division by zero
363+
if (scaledSpaceWidth > 0 && gap > scaledSpaceWidth * 0.3) {
363364
const numSpaces = Math.round(gap / scaledSpaceWidth);
364365
prevText.str += ' '.repeat(Math.max(1, numSpaces));
365366
}

lib/pdfcanvas.js

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -436,8 +436,33 @@ export default class CanvasRenderingContext2D_ {
436436
}
437437

438438
// Method signature matches Canvas API
439+
// MQZ. 2025.01: Enhanced to support transparency groups which handled by base/display/canvas.js when invokes endGroup()
440+
// This handles transparency groups where text/lines/fills are rendered to a temporary canvas and then composited back to the main canvas via drawImage()
439441
drawImage(image, var_args) {
440-
//MQZ. no image drawing support for now
442+
// If source has our custom data arrays (i.e., it's a PDFCanvas), merge them to this canvas
443+
if (image && typeof image === "object") {
444+
// Merge Texts array
445+
if (image.Texts && Array.isArray(image.Texts) && image.Texts.length > 0) {
446+
this.canvas.Texts.push(...image.Texts);
447+
image.Texts = []; // Clear source after merging
448+
}
449+
// Merge HLines array
450+
if (image.HLines && Array.isArray(image.HLines) && image.HLines.length > 0) {
451+
this.canvas.HLines.push(...image.HLines);
452+
image.HLines = [];
453+
}
454+
// Merge VLines array
455+
if (image.VLines && Array.isArray(image.VLines) && image.VLines.length > 0) {
456+
this.canvas.VLines.push(...image.VLines);
457+
image.VLines = [];
458+
}
459+
// Merge Fills array
460+
if (image.Fills && Array.isArray(image.Fills) && image.Fills.length > 0) {
461+
this.canvas.Fills.push(...image.Fills);
462+
image.Fills = [];
463+
}
464+
}
465+
// Note: actual pixel drawing is not supported (no-op for regular images)
441466
}
442467

443468
getImageData(x, y, w, h) {

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "pdf2json",
3-
"version": "4.0.1",
3+
"version": "4.0.2",
44
"description": "PDF file parser that converts PDF binaries to JSON and text, powered by porting a fork of PDF.JS to Node.js",
55
"keywords": [
66
"pdf",
@@ -47,7 +47,7 @@
4747
"parse-tb": "./bin/pdf2json.js -f ./test/pdf/misc/i242_testingWithTable.pdf -o ./test/target/misc",
4848
"parse-tc": "./bin/pdf2json.js -f ./test/pdf/misc/i293_pdfpac.pdf -o ./test/target/misc",
4949
"parse-rectFix": "./bin/pdf2json.js -f ./test/pdf/misc/pr298_rect_fix_from_upstream.pdf -o ./test/target/misc",
50-
"parse-e": "./bin/pdf2json.js -f ./test/pdf/misc/i418_precompilato_fake.pdf -o ./test/target/misc",
50+
"parse-e": "./bin/pdf2json.js -f ./test/pdf/misc/i418_precompilato_fake.pdf -o ./test/target/misc -c",
5151
"build:rollup": "npx rollup -c ./rollup.config.js",
5252
"build:bundle-pdfjs-base": "node rollup/bundle-pdfjs-base.js",
5353
"build": "npm run build:bundle-pdfjs-base && npm run build:rollup",
@@ -121,4 +121,4 @@
121121
"readme.md",
122122
"license.txt"
123123
]
124-
}
124+
}

0 commit comments

Comments
 (0)