Reputation: 2777
I'm new in xhtml2pdf with python , write html code with python in readable format is somehow cumbersome . I want to know how to include .html file in pisa document.Here is simple code in which I create pdf file:
from xhtml2pdf import pisa
def main():
filename = "simplePrint.pdf"
# generate content
xhtml = "<h1 align='center'>Test print</h1>\n"
xhtml += "<h2>This is printed from within a Python application</h2>\n"
xhtml += "<p style=\"color:red;\">Coloured red using css</p>\n"
pdf = pisa.CreatePDF(xhtml, file(filename, "w"))
if __name__ == "__main__":
main()
If code of html is too large I want to make an another module, place it in myHtml.html file and include in 3rd last line like:
pdf = pisa.CreatePDF(myHtml.html, file(filename, "w"))
How I reach this problem?
Upvotes: 1
Views: 4112
Reputation: 3331
If I understand you correctly, then what you want is probably another file in the same directory as your above code called myHtml.py
looking like this:
html = "<h1 align='center'>Test print</h1>\n"
html += "<h2>This is printed from within a Python application</h2>\n"
html += "<p style=\"color:red;\">Coloured red using css</p>\n"
and then in your above code import myHtml
in the beginning.
However, I believe it would be better, if you put the html into a html file, called myTemplate.html
for example. Then you could read the file like this:
template = open("myTemplate.html")
pdf = pisa.CreatePDF(template.read(), file(filename, "w"))
template.close()
Upvotes: 1