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

Aperta Adatta ai principianti
#442 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
1/5
Tempo stimato
1-3 ore
Idoneità per principianti
90/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
javascript, node.js
Ambito
backend, data

Direzione di ricerca

Leggi lib/pdftextsorter.js ed esamina l’ordinamento delle righe descritto nell’issue. Verifica in che modo le coordinate y di bidiTexts corrispondono all’ordine visivo della pagina, poi esegui la riproduzione fornita con tre righe e verifica che getRawTextContent() emetta First, Second, Third line in quest’ordine, seguito dall’interruzione di pagina.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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/

Lingua principale
Java
Stelle
2.2k
Fork
396
Merge medio
9g 19h
PR unite (30g)
3

Preparare l'ambiente

Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di modesty/pdf2json

Tutte le issue di modesty/pdf2json

Issue simili

Altre issue su Java

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.