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

Open Beginner friendly
#442 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
1/5
Estimated time
1-3 hours
Newbie friendliness
90/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
javascript, node.js
Domain
backend, data

Research direction

Read lib/pdftextsorter.js and inspect the line sort described in the issue. Check how the bidiTexts y coordinates relate to visual page order, then run the supplied three-line reproduction and verify getRawTextContent() emits First, Second, Third line in that order, followed by the page break.

Written by the indexing model from the issue text.

Description

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/

Dominant language
Java
Stars
2.2k
Forks
396
Avg merge
9d 19h
Merged PRs (30d)
3

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from modesty/pdf2json

All issues in modesty/pdf2json

Similar issues

More Java issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.