-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_create_pdf_document.py
More file actions
77 lines (61 loc) · 2.58 KB
/
example_create_pdf_document.py
File metadata and controls
77 lines (61 loc) · 2.58 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import aspose.pdf as ap
import io
import pytesseract
import sys
from os import path
from pathlib import Path
sys.path.append(path.join(path.dirname(__file__), ".."))
from config import set_license, initialize_data_dir # noqa: E402
def create_new_document(input_pdf, output_pdf):
"""Create a simple PDF with a single “Hello World!” page."""
document = ap.Document()
page = document.pages.add()
page.paragraphs.add(ap.text.TextFragment("Hello World!"))
document.save(output_pdf)
def create_searchable_document(infile, outfile, image_file_path, page_number=1):
"""
An example of using optical character recognition (OCR) technology to create a searchable PDF document.
Args:
infile (str): The name of the input PDF file
outfile (str): The base name for output files (index will be appended)
image_file_path (str): The name of the image file
page_number (int): The page number
Returns:
None
"""
image_stream = io.FileIO(image_file_path, 'x')
try:
document = ap.Document(infile)
resolution = ap.devices.Resolution(300)
png_device = ap.devices.PngDevice(resolution)
png_device.process(document.pages[page_number], image_stream)
pdf = pytesseract.image_to_pdf_or_hocr(image_file_path, extension='pdf')
document = ap.Document(io.BytesIO(pdf))
document.save(outfile)
finally:
image_stream.close()
image_file = Path(image_file_path)
image_file.unlink(missing_ok=True)
def run_all_examples(data_dir=None, license_path=None):
"""Run PDF creation examples and report status."""
set_license(license_path)
input_dir, output_dir = initialize_data_dir(data_dir)
examples = [
("Create new document", create_new_document),
("Create a Searchable PDF document", create_searchable_document),
]
for name, func in examples:
try:
input_file_name = path.join(input_dir, f"{func.__name__}.pdf")
output_file_name = path.join(output_dir, f"{func.__name__}.pdf")
if func == create_searchable_document:
image_path = path.join(output_dir, "create_searchable_document.png")
func(input_file_name, output_file_name, image_path)
else:
func(input_file_name, output_file_name)
print(f"✅ Success: {name}")
except Exception as e:
print(f"❌ Failed: {name} - {str(e)}")
print("\nAll PDF creation examples finished.")
if __name__ == "__main__":
run_all_examples()