pythonpdfpdfminerpdf-to-html

PDFminer - Is there a way to convert pdf into html from pdfminer?


Is a simple way to convert pdf to html using pdfminer? I have seen many questions like this but they won't give me a right answer...

I have entered this in my ConEmu prompt:

# pdf2txt.py -o output.html -t html sample.pdf
usage: C:\Program Files\Python37-32\Scripts\pdf2txt.py [-P password] [-o output] [-t text|html|xml|tag] [-O output_dir] [-c encoding] [-s scale] [-R rotation] [-Y normal|loose|exact] [-p pagenos] [-m maxpages] [-S] [-C] [-n] [-A] [-V] [-M char_margin] [-L line_margin] [-W word_margin] [-F boxes_flow] [-d] input.pdf ...

I hope that is not the response i should get from pdf2txt.py..

Is there any code snippet that will work? I have tried this:

from pdfminer.pdfinterp import PDFResourceManager, PDFPageInterpreter
from pdfminer.converter import HTMLConverter
from pdfminer.layout import LAParams
from pdfminer.pdfpage import PDFPage
from io import BytesIO


def convert_pdf_to_html(path):
    rsrcmgr = PDFResourceManager()
    retstr = BytesIO()
    codec = 'utf-8'
    laparams = LAParams()
    device = HTMLConverter(rsrcmgr, retstr, codec=codec, laparams=laparams)
    fp = open(path, 'rb')
    interpreter = PDFPageInterpreter(rsrcmgr, device)
    password = ""
    maxpages = 0 #is for all
    caching = True
    pagenos=set()
    for page in PDFPage.get_pages(fp, pagenos, maxpages=maxpages, password=password,caching=caching, check_extractable=True):
        interpreter.process_page(page)
    fp.close()
    device.close()
    str = retstr.getvalue()
    retstr.close()
    return str

test = convert_pdf_to_html('E://sample.pdf')

But it didn't give me any html file nor any output

and another code:

import pdfminer
from pdfminer.pdfinterp import PDFResourceManager, process_pdf
from pdfminer.converter import HTMLConverter, TextConverter
from pdfminer.layout import LAParams
rsrcmgr = PDFResourceManager()
laparams = LAParams()
converter = HTMLConverter if format == 'html' else TextConverter
device = converter(rsrcmgr, out_file, codec='utf-8', laparams=laparams)
process_pdf(rsrcmgr, device, in_file, pagenos=[1,3,5], maxpages=9)

with contextlib.closing(tempfile.NamedTemporaryFile(mode='r', suffix='.xml')) as xmlin:
    cmd = 'pdftohtml -xml -nodrm -zoom 1.5 -enc UTF-8 -noframes "%s" "%s"' % (
            pdf_filename, xmlin.name.rpartition('.')[0])
    os.system(cmd + " >/dev/null 2>&1")
    result = xmlin.read().decode('utf-8')

It gives this:

Traceback (most recent call last):

  File "E:\Blah\blah\blah.py", line 2, in <module>
    from pdfminer.pdfinterp import PDFResourceManager, process_pdf

ImportError: cannot import name 'process_pdf' from 'pdfminer.pdfinterp' (c:\program files\python37-32\lib\site-packages\pdfminer\pdfinterp.py)

Info:

System : Windows 7 SP-1 32-bit
Python : 3.7.0
PDFMiner : 20191125

Solution

  • Install pdfminer and convert pdf to html using below command

    $ pip3 install pdfminer
    $ pdf2txt.py  -o output.html document.pdf