DEV Community

Cover image for How I Prepare PDF Text for an Audiobook
cleora
cleora

Posted on

How I Prepare PDF Text for an Audiobook

Part 1 of Building All to Audiobook

Our sample PDF has a running "CHAPTER ONE" header above "Chapter II. The shore." I remove the repeated margin text before narration, keeping the chapter title and story.

Building All to Audiobook

I ran into these problems while building All to Audiobook for my mom. After neck surgery, holding a book became difficult for her, and many of the ebooks she wanted to read had no audiobook edition. I wanted her to turn books she already had into audio she could keep and listen to in her usual player.

The app processes documents and generates speech on the reader's own device, then exports MP3 or chaptered M4B files. This series follows the practical problems I encountered while building it, the approaches I chose, and what still needs work.

I use PDF.js for the extraction step in All to Audiobook's PDF-to-audio converter. It gives me the words and their positions on the page. I keep the positions and text sizes for cleanup.

Here's the opening of page two before and after removing the margins:

Stage Opening text
Extracted from the PDF CHAPTER ONE - PDF READING SAMPLE · 2 · Chapter II. The shore. Nora took her coat from the hook and stepped outside.
After margin cleanup Chapter II. The shore. Nora took her coat from the hook and stepped outside.

The dots in the first row separate extracted text blocks.

Page two of the sample PDF beside its cleaned text. Orange outlines mark the removed running header and page number; the green outline marks the retained chapter title.

The running header appears near the top of both pages. The chapter title appears once, farther down. I compare margin lines across the document and count the pages on which each line appears.

For this comparison, I ignore capitalization and replace digit sequences with a placeholder. A header can then match even when it includes a changing page number. A line needs to recur across enough pages before I remove it. I handle standalone page numbers separately, within the margins.

"Chapter II. The shore." survives those checks. So does Nora's opening sentence. Matching the word "Chapter" alone would have removed text I wanted to keep.

A repeated margin line can contain something the reader wants to hear. There is a header control for that choice. Turning removal off brings the running heading back in this sample.

Footnotes need a choice too.

I estimate the body text size from the longer lines on each page. Then I look for smaller text near the bottom that starts with a number or note symbol. Following small-print lines can continue the same note. The comparison uses that page's body size, so it can work across books with different typography.

Detection only tells me that a passage looks like a note. The reader decides what to do with it.

One PDF test includes "1 An explanatory footnote below the text," positioned beneath the body in smaller type. Here is what the three settings produce from that input:

Setting What happens to the note
Keep The note remains in the reading text.
Skip The note is omitted.
At the end The note appears after the chapter body, introduced by "Notes."

Moving notes to the end lets someone hear the main passage first and still get the extra information. Skipping them suits a different reading preference. I don't want the detector making that decision for everyone.

The rule depends on the note's layout. It can miss notes or mistake other small text for one. The preview shows the result before conversion, and Keep leaves the note text in place.

The original extraction stays available.

That follows from the footnote controls. A reader who changes Skip to Keep expects the notes to reappear. Deleting them from the only stored copy would make that impossible.

Each chapter retains its extracted page data. The cleanup settings produce a separate reading version. When a setting changes, I rebuild that version from the source instead of editing the previous result again.

Skip, then At the end, then Keep should therefore produce the same text as choosing Keep at the start. The intermediate choices don't permanently remove anything from the source.

I can test those choices against the same input. The sample's expected output includes the actual chapter title and Nora's complete opening sentence. For a moved footnote, I also need to check that the note appears after the chapter body. A shorter result alone would tell me very little.

PDF bookmarks provide chapter boundaries where possible. Otherwise, the importer looks for chapter headings and falls back to groups of whole pages. Pronunciation replacements happen before the prepared text reaches the voice model.

These steps need selectable text. Scanned pages require OCR, which the app doesn't include. For a supported file, the reader can inspect the prepared chapters and change the cleanup settings before generating audio.

If you have a text-based PDF you'd like to listen to, try All to Audiobook with one chapter. Check the preview with the footnotes kept, skipped, or moved to the end. I'd like to hear where the result differs from what you wanted to listen to.

Next in the series: How I Resume an Audiobook Conversion After Closing the Tab.

Top comments (1)

Collapse
 
userxnom profile image
Abdullah Naseer •

The footnote controls stood out to me — letting the reader choose Keep, Skip, or At the end is a thoughtful touch for listening, since a footnote read mid-sentence would break the flow. One edge case I wondered about: how does the cleanup handle words split by hyphens across line breaks? PDF text extraction often leaves those as two fragments, and a narrator reading them separately would sound odd. Is that something the preparation step rejoins, or does it mostly come out clean with the sample PDFs you tested?