import aspose.pdf as ap
import xml.etree.ElementTree as ET
from io import BytesIO
def example_xslt_to_pdf():
data_dir = "C:/tmp/"
with open("XMLFile1.xml", "r") as xml_file:
xml_content = xml_file.read()
with open("XSLTFile1.xslt", "r") as xslt_file:
xslt_content = xslt_file.read()
options = ap.HtmlLoadOptions()
# Set page size to A5
options.page_info.height = 595
options.page_info.width = 420
pdf_document = ap.Document(transform_xml_to_html(xml_content, xslt_content), options)
pdf_document.save(data_dir + "data_xml.pdf")
def transform_xml_to_html(input_xml, xslt_string):
transform = ET.XSLT(ET.XML(xslt_string))
xml_tree = ET.ElementTree(ET.XML(input_xml))
result_tree = transform(xml_tree)
html_str = ET.tostring(result_tree, encoding='unicode', method='html')
return BytesIO(html_str.encode('utf-8'))
example_xslt_to_pdf()