getRawTextContent() returns the lines of every page in reverse order (bottom to top) since 4.0.3

オープン 初心者向け
#442 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

評価

難易度
1/5
見積もり時間
1〜3時間
初心者へのやさしさ
90/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
javascript, node.js
領域
backend, data

調査の方向性

lib/pdftextsorter.js を読み、issue で説明されている行のソートを調べてください。bidiTexts の y 座標がページの視覚的な順序とどう対応するかを確認し、次に提示された3行の再現例を実行して、getRawTextContent() が First, Second, Third line の順に出力し、その後にページ区切りを出力することを確認してください。

索引モデルが issue の本文から書いたものです。

説明

Since 4.0.3, getRawTextContent() (and the CLI's -c content output) emits each page's lines from the bottom of the page to the top. A plain three-line page comes out last line first.

Reproduction (npm i [email protected], run with node repro.mjs):

import fs from "node:fs";
import PDFParser from "pdf2json";

// one page, three lines drawn top to bottom (y = 700, 680, 660)
const content = "BT /F1 12 Tf 72 700 Td (First line) Tj 0 -20 Td (Second line) Tj 0 -20 Td (Third line) Tj ET";
const objs = [
  "<< /Type /Catalog /Pages 2 0 R >>",
  "<< /Type /Pages /Kids [3 0 R] /Count 1 >>",
  "<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 792] /Resources << /Font << /F1 4 0 R >> >> /Contents 5 0 R >>",
  "<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica >>",
  `<< /Length ${content.length} >>\nstream\n${content}\nendstream`,
];
let pdf = "%PDF-1.4\n";
const offsets = objs.map((o, i) => { const off = pdf.length; pdf += `${i + 1} 0 obj\n${o}\nendobj\n`; return off; });
const xref = pdf.length;
pdf += `xref\n0 ${objs.length + 1}\n0000000000 65535 f \n` + offsets.map(o => `${String(o).padStart(10, "0")} 00000 n \n`).join("");
pdf += `trailer\n<< /Size ${objs.length + 1} /Root 1 0 R >>\nstartxref\n${xref}\n%%EOF\n`;

const parser = new PDFParser(null, true);
parser.on("pdfParser_dataError", (e) => console.error(e.parserError));
parser.on("pdfParser_dataReady", () => console.log(JSON.stringify(parser.getRawTextContent())));
fs.writeFileSync("lines.pdf", pdf, "latin1");
parser.loadPDF("lines.pdf");

Actual (pdf2json 4.0.3, 4.1.0, 4.1.1 / master 5edaa03 on Node v26.13.0):

"Third line\r\nSecond line\r\nFirst line\r\n----------------Page (0) Break----------------\r\n"

Expected (what 4.0.2 and pdftotext lines.pdf - produce):

"First line\r\nSecond line\r\nThird line\r\n----------------Page (0) Break----------------\r\n"

The same happens with ordinary PDFs, such as the Google Docs sample.pdf attached to #421 (comment of 2026-04-28).

The spatial sort added in #422 (lib/pdftextsorter.js, lines.sort((a, b) => a[0].y - b[0].y)) sorts lines by ascending y, but bidiTexts y coordinates are in PDF user space where y grows upward, so ascending y orders lines from bottom to top instead of top to bottom (lines.sort((a, b) => b[0].y - a[0].y)).

Credit: https://ucsbplse.github.io/

主要言語
Java
スター
2.2k
フォーク
396
平均マージ
9日 19時間
マージ済み PR(30日)
3

環境構築

このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

modesty/pdf2json のほかの issue

modesty/pdf2json の issue をすべて見る

似ている issue

Java の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。