Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 0 additions & 67 deletions .github/workflows/npm-publish.yml

This file was deleted.

2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2024 Utkarsh Pancholi
Copyright (c) 2025 Javier Bagatoli

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
182 changes: 149 additions & 33 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,58 +1,174 @@
# react-pdftotext
# react-pdftotext-advanced

Light-weight memory-safe client library for extracting plain text from pdf files.
A browser-based PDF text extraction library that preserves the original reading experience by reconstructing paragraphs and page breaks.

## Installing
Unlike the original `react-pdftotext`, which returns a continuous stream of text, **react-pdftotext-advanced** analyzes the extracted content to preserve paragraph separation and page spacing, producing text that is much closer to the original document.

Using npm:
## Why?

```js
npm install react-pdftotext
Most browser PDF extraction libraries prioritize extracting text, not readability.

For example, a document like this:

```text
Good morning everyone.

How are you all?

I hope you're well.
```

## Example
may become:

**Local File Input**
```text
Good morning everyone.How are you all?I hope you're well.
```

Now add a input tag with type="file" to take file input.
With **react-pdftotext-advanced**, the output becomes:

```html
<input type="file" accept="application/pdf" onChange={extractText} />
```text
Good morning everyone.

How are you all?

I hope you're well.
```

Import the pdf2text function from package
This makes the extracted text much easier to:

- Read
- Display
- Process with AI models (LLMs)
- Index
- Summarize
- Search

---

# Features

```js
import pdfToText from "react-pdftotext";
- Preserves paragraph separation.
- Detects page endings.
- Produces human-readable output.
- Runs entirely in the browser.
- No backend required.
- Promise-based API.
- Easy integration with React applications.

---

# Installation

```bash
npm install react-pdftotext-advanced
```

---

# Basic Usage

```tsx
import pdfToText from "react-pdftotext-advanced";

function extractText(event) {
const file = event.target.files[0];
pdfToText(file)
.then((text) => console.log(text))
.catch((error) => console.error("Failed to extract text from pdf"));
const file = event.target.files[0];

pdfToText(file, "advanced")
.then((text) => console.log(text))
.catch((error) => console.error(error));
}
```

**Remote PDF File Input**
```html
<input
type="file"
accept="application/pdf"
onChange={extractText}
/>
```

For Pdf files stored at remote locations
---

```js
import pdfToText from 'react-pdftotext'
# Extraction Modes

const pdf_url = "REMOTE_PDF_URL"
## Simple

function extractText() {
const file = await fetch(pdf_url)
.then(res => res.blob())
.catch(error => console.error(error))
Returns the text using the original extraction behavior.

pdfToText(file)
.then(text => console.log(text))
.catch(error => console.error("Failed to extract text from pdf"))
}
```ts
pdfToText(file, "simple");
```

Output

```text
Good morning everyone.How are you all?I hope you're well.
```

---

## Advanced

Reconstructs paragraph breaks and page spacing for improved readability.

```ts
pdfToText(file, "advanced");
```

Output

```text
Good morning everyone.

How are you all?

I hope you're well.
```

## Contributing
---

# Use Cases

This library is particularly useful for:

- Reading letters and documents
- AI preprocessing (LLMs)
- Retrieval-Augmented Generation (RAG)
- Search indexing
- Document summarization
- PDF viewers
- Educational platforms

---

# How It Works

After extracting the raw text from the PDF, the library analyzes spacing and page transitions to reconstruct the document's logical reading order.

Instead of returning a continuous stream of text, it attempts to preserve the visual structure that a reader expects.

---

# Browser Support

Supports modern browsers that are compatible with PDF.js.

---

# Credits

This project is based on the excellent work of **react-pdftotext** and extends its functionality by improving the readability of the extracted text.

---

# Contributing

Contributions, bug reports and feature requests are always welcome.

If you have ideas for improving text reconstruction or supporting additional document layouts, feel free to open an issue or submit a pull request.

---

# License

This project welcomes contributions and suggestions.
MIT
17 changes: 17 additions & 0 deletions aux-funcitons.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const calculateStartLine = (controlPosX: {[key: string]: number}): number => {
let vectorKeys = Object.keys(controlPosX);
let moreUsualValue: {key:string, value: number} = {key:'', value: 0};

vectorKeys.forEach(key => {
if(controlPosX[key] > moreUsualValue.value){
moreUsualValue = {
key,
value: controlPosX[key] as number
}
}
})

return (Number(moreUsualValue.key)+2)
}

export default calculateStartLine;
Loading