-
Notifications
You must be signed in to change notification settings - Fork 8
/
pdf2html.py
executable file
·48 lines (40 loc) · 1.33 KB
/
pdf2html.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/usr/bin/env python3
"""
PDF to HTML conversion - first step of the process.
Batch processes a folder full of PDFs using pdf2htmlEX
producing an HTML folder.
This HTML uses just CSS positioning for layout. We need
further work to add semantic tags: transcript.py
"""
import glob
import multiprocessing
import os
import time
from pathlib import Path
import config
def pdf2html(pdf_path):
"""Generates a long command"""
fn = Path(pdf_path).name.replace('.pdf', '')
# --embed cfijo = don't embed Css, Fonts, Images, Js, Outlines
# > man pdf2htmlEX
if config.DOCKER_INSTALL:
pdf2htm = f"docker run -ti --rm -v {config.DATA_DIR}:/pdf -w /pdf {config.DOCKER_IMG_TAG}"
out_dir = '/pdf/HTML'
pdf_path = pdf_path.replace(config.PDF_DIR, '/pdf/PDF')
hint = ""
else:
pdf2htm = 'pdf2htmlEX'
out_dir = config.HTML_DIR
hint = " --external-hint-tool ttfautohint"
cmd = (f"{pdf2htm} --embed-external-font 0 {hint}"
" --process-nontext 0 --embed cfijo"
f" --dest-dir {os.path.join(out_dir, fn)} {pdf_path} {fn}.html")
print()
print(cmd)
os.system(cmd)
time.sleep(.2)
if __name__ == '__main__':
os.makedirs(config.HTML_DIR, exist_ok=True)
p = multiprocessing.Pool(4)
pdfs = glob.glob(config.PDF_DIR + '/*.pdf')
p.map(pdf2html, pdfs)