Answers.Body: <p>The answer is that Calibre is going to give you the best results on converting a PDF to an ePub with the minimum of pain, and then you can use the editor in Calibre to clean up any issues. Trying to start from scratch with the base text extracted from the PDF is going to involve a LOT of cleaning things up manually that Calibre can often do on its own.</p>
  <p>That said, PDF is the worst of all eText formats because it is, as @drmishe-pippik said, a PAGE format, not a DOCUMENT format. PDF is great when you want to distribute something that is going to end up on a physical piece of paper, assuming you make the PDF to match the physical site of the paper someone will print on (Look up dealing with US Letter v A4 for lots more &quot;Fun With PDFs!”), but it is not at all a good format for producing an ebook that allows changing the font size, margins, width, styling, etc.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-04-25T02:25:23.903Z
Answers.LastActivityDate: 2022-04-25T02:25:23.903Z
Answers.UserDisplayName: lbutlr
Answers.Body: <p>Because PDF describes <em>page formatting</em>, rather than being an editable, smoothly-flowing document, it is difficult to convert to word-processing document format, such as Word's <em>DOCX</em> and LibreOffice's <em>ODT</em>. In fact, some PDF files have <em>no</em> text, only <em>images</em> of the text, so optical character recognition (OCR) is needed to convert the image back to text. Such conversion needs some &quot;artificial intelligence&quot; to make a decent document.</p>
  <p>The cleanest and most accurate conversion I've seen is using online sites to convert PDF to document format, such as  <a href="https://pdf2doc.com/" rel="nofollow noreferrer">PDF2Doc</a>, <a href="https://www.freeconvert.com/pdf-to-doc" rel="nofollow noreferrer">FreeConvert</a> and <a href="https://www.zamzar.com/convert/pdf-to-doc/" rel="nofollow noreferrer">Zamzar</a>. Once the converted file is downloaded, <em>Calibre</em> does a very good job of converting DOCX, ODT and similar word-processing documents to EPUB and other etext publishing formats.</p>
  <p>Of course, this two-step process is a bit of a nuisance, it exposes your text to a third party, and the conversion may still be <em>far</em> from perfect, particularly where OCR is used ... but it's usually faster and easier than converting PDF to EPUB manually.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-06-01T01:59:04.563Z
Answers.LastActivityDate: 2021-06-01T01:59:04.563Z
Answers.UserDisplayName: DrMoishe Pippik
Body: <p>I've read all similar questions here, and most of the answers suggest using <code>calibre</code> for this task, however, I'm trying to improve the output. Here I explain:</p>
  <p>I've extracted text from a PDF using <code>pdftohtml</code> (part of Poppler) using -c and -s options. I got an HTML that contains text and <strong>surprisingly</strong> text is correctly un-wrapped, so the main problem of extracting text from PDFs is gone. If I feed this Html to calibre (specifically to <code>ebook-convert</code>) I get a dirty epub with the following problems that need to be solved:</p>
  <ul>
  <li>the title of the document repeats at header of every page -&gt; it should be removed</li>
  <li>page numbers were in the pdf and appear in the epub, that make no sense -&gt; they should be removed</li>
  <li>footnotes appear as normal text -&gt; they should be recognized as footnotes and properly linked</li>
  <li>text is &quot;dirty&quot;: there are many different classes for <code>&lt;p&gt;</code> paragraphs, with &quot;<code>absolute position</code>&quot; attributes, leading to messy text: sentences are in the correct order, but the different settings for every class make the text rendered slightly under, above, before or after (a few pixels) the place where it should be -&gt; all the classes should be removed and replaced only with <code>&lt;b&gt; and &lt;i&gt;</code> when needed</li>
  <li>titles appear in bold, and are somehow recognized by calibre heuristic conversion, however, this process is not always correctly performed</li>
  <li>additionally, no table of contents is created, and should be</li>
  </ul>
  <p>Is there any software or library that performs these tasks? I'm not a developer, but I'd rather code something than editing every book by hand. I can't believe that in 2021 there's not a program that does that, since open source world is so vast and rich with every kind of tools. I think that what I'm explaining is a very common need, and that thousands of people have felt the same need. Looking forward to hear your suggestions.</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2021-05-03T10:48:10.587Z
Comments.Text: The issue with at least the first 3 points is that they are in the text of the pdf - There is no general rule to recognise plain text from the cases you have given. It is all text. The reason for there not being code to do this is people will look for the source that created the pdf or a version in a structured format e.g. epub and that works in most cases. Getting structured information from a pdf is a difficult problem. You can probably write scripts for each book to remove page numbers and title as they will be constant in the book
Comments.UserDisplayName: mmmmmm
Tags: <pdf><conversion><footnotes><cleaning>
Title: Convert PDF to Epub, advanced features
UserDisplayName: Attilio
ThreadText: # Convert PDF to Epub, advanced features
  
  (Asked by: username_0 on 2021-04-29)
  
  I've read all similar questions here, and most of the answers suggest using `calibre` for this task, however, I'm trying to improve the output. Here I explain:
  
  I've extracted text from a PDF using `pdftohtml` (part of Poppler) using -c and -s options. I got an HTML that contains text and **surprisingly** text is correctly un-wrapped, so the main problem of extracting text from PDFs is gone. If I feed this Html to calibre (specifically to `ebook-convert`) I get a dirty epub with the following problems that need to be solved:
  
  * the title of the document repeats at header of every page -> it should be removed
  * page numbers were in the pdf and appear in the epub, that make no sense -> they should be removed
  * footnotes appear as normal text -> they should be recognized as footnotes and properly linked
  * text is "dirty": there are many different classes for  paragraphs, with "`absolute position`" attributes, leading to messy text: sentences are in the correct order, but the different settings for every class make the text rendered slightly under, above, before or after (a few pixels) the place where it should be -> all the classes should be removed and replaced only with  `and`  when needed
  * titles appear in bold, and are somehow recognized by calibre heuristic conversion, however, this process is not always correctly performed
  * additionally, no table of contents is created, and should be
  
  Is there any software or library that performs these tasks? I'm not a developer, but I'd rather code something than editing every book by hand. I can't believe that in 2021 there's not a program that does that, since open source world is so vast and rich with every kind of tools. I think that what I'm explaining is a very common need, and that thousands of people have felt the same need. Looking forward to hear your suggestions.
  
  **Tags:** pdf, conversion, footnotes, cleaning
  
  > **Comments:**
  > **username_1**: The issue with at least the first 3 points is that they are in the text of the pdf - There is no general rule to recognise plain text from the cases you have given. It is all text. The reason for there not being code to do this is people will look for the source that created the pdf or a version in a structured format e.g. epub and that works in most cases. Getting structured information from a pdf is a difficult problem. You can probably write scripts for each book to remove page numbers and title as they will be constant in the book  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_2 on 2022-04-25. Score: 1
  
  The answer is that Calibre is going to give you the best results on converting a PDF to an ePub with the minimum of pain, and then you can use the editor in Calibre to clean up any issues. Trying to start from scratch with the base text extracted from the PDF is going to involve a LOT of cleaning things up manually that Calibre can often do on its own.
  
  That said, PDF is the worst of all eText formats because it is, as @drmishe-pippik said, a PAGE format, not a DOCUMENT format. PDF is great when you want to distribute something that is going to end up on a physical piece of paper, assuming you make the PDF to match the physical site of the paper someone will print on (Look up dealing with US Letter v A4 for lots more "Fun With PDFs!”), but it is not at all a good format for producing an ebook that allows changing the font size, margins, width, styling, etc.
  
  
  ---
  
  ### Answer by username_3 on 2021-06-01. Score: 1
  
  Because PDF describes *page formatting*, rather than being an editable, smoothly-flowing document, it is difficult to convert to word-processing document format, such as Word's *DOCX* and LibreOffice's *ODT*. In fact, some PDF files have *no* text, only *images* of the text, so optical character recognition (OCR) is needed to convert the image back to text. Such conversion needs some "artificial intelligence" to make a decent document.
  
  The cleanest and most accurate conversion I've seen is using online sites to convert PDF to document format, such as [PDF2Doc](https://pdf2doc.com/), [FreeConvert](https://www.freeconvert.com/pdf-to-doc) and [Zamzar](https://www.zamzar.com/convert/pdf-to-doc/). Once the converted file is downloaded, *Calibre* does a very good job of converting DOCX, ODT and similar word-processing documents to EPUB and other etext publishing formats.
  
  Of course, this two-step process is a bit of a nuisance, it exposes your text to a third party, and the conversion may still be *far* from perfect, particularly where OCR is used ... but it's usually faster and easier than converting PDF to EPUB manually.
  
  
  ---
  
  