Skip to content

Commit

Permalink
fix: #165 : check and make buffer before parse
Browse files Browse the repository at this point in the history
  • Loading branch information
modesty committed Aug 17, 2024
1 parent 115d618 commit 6a5d3f7
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pdf2json",
"version": "3.1.4",
"version": "3.1.5",
"description": "PDF file parser that converts PDF binaries to JSON and text, powered by porting a fork of PDF.JS to Node.js",
"keywords": [
"pdf",
Expand Down
16 changes: 13 additions & 3 deletions pdfparser.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import fs from "fs";
import nodeUtil from "util";
import { readFile } from "fs/promises";
import { EventEmitter } from "events";
import { Buffer } from "buffer";

import PDFJS from "./lib/pdf.js";
import { ParserStream, StringifyStream } from "./lib/parserstream.js";
Expand Down Expand Up @@ -231,11 +232,20 @@ export default class PDFParser extends EventEmitter {
/**
* Parse PDF buffer. Introduce a way to directly process buffers without the need to write it to a temporary file
* @param {Buffer} pdfBuffer - PDF buffer
* @param {number} verbosity - Verbosity level
* @param {number} verbosity - Verbosity level, ERRORS = 0, WARNINGS = 1, INFOS = 5;
*/
parseBuffer(pdfBuffer, verbosity) {
nodeUtil.verbosity(verbosity || 0);
this.#startParsingPDF(pdfBuffer);
nodeUtil.verbosity(verbosity); // validated in util.js
if ((!pdfBuffer?.length) || (!pdfBuffer.buffer)) {
nodeUtil.p2jerror("Error: empty PDF buffer, nothing to parse.");
return;
}
let pdfBufferParse = pdfBuffer;
if (pdfBufferParse.buffer.byteLength !== pdfBufferParse.length) {
pdfBufferParse = Buffer.from(pdfBufferParse.buffer, 0, pdfBufferParse.byteLength);
}

this.#startParsingPDF(pdfBufferParse);
}

/**
Expand Down

0 comments on commit 6a5d3f7

Please sign in to comment.