-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_save.py
More file actions
68 lines (52 loc) · 2.07 KB
/
Copy pathexample_save.py
File metadata and controls
68 lines (52 loc) · 2.07 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
import aspose.pdf as ap
import io
import sys
from os import path
sys.path.append(path.join(path.dirname(__file__), ".."))
from config import set_license, initialize_data_dir
def save_document_to_file(infile, outfile):
document = ap.Document(infile)
# make some manipulation, e.g. add new empty page
document.pages.add()
document.save(outfile)
def save_document_to_stream(infile, outfile):
document = ap.Document(infile)
# make some manipulation, e.g. add new empty page
document.pages.add()
with io.FileIO(outfile, "w") as stream:
document.save(stream)
def save_document_as_standard(infile, outfile, logfile):
document = ap.Document(infile)
document.pages.add()
document.convert(logfile, ap.PdfFormat.PDF_X_3, ap.ConvertErrorAction.DELETE)
document.save(outfile)
def run_all_examples(data_dir=None, license_path=None):
"""Run Save Document examples and report status.
Args:
data_dir (str, optional): Input/output directory override.
license_path (str, optional): Path to Aspose.PDF license file.
Returns:
None
"""
set_license(license_path)
input_dir, output_dir = initialize_data_dir(data_dir)
examples = [
("Save document to file", save_document_to_file),
("Save document to stream", save_document_to_stream),
("Save document as standard", save_document_as_standard),
]
for name, func in examples:
try:
input_file_name = path.join(input_dir, "sample3.pdf")
output_file_name = path.join(output_dir, f"{func.__name__}_out.pdf")
if func == save_document_as_standard:
log_file_name = path.join(output_dir, f"{func.__name__}_out.log")
func(input_file_name, output_file_name, log_file_name)
else:
func(input_file_name, output_file_name)
print(f"✅ Success: {name}")
except Exception as e:
print(f"❌ Failed: {name} - {str(e)}")
print("\nAll Save Document examples finished.")
if __name__ == "__main__":
run_all_examples()