1

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?

1 Answer 1

1

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()
Sign up to request clarification or add additional context in comments.

5 Comments

If I want to insert python variable(state) in HTML code.Then how?
That depends a lot on how sophisticated your insertions have to be. If it's just some strings, you could read the template, have some unique strings there like %INSERTCOLORHERE%, then use a regex or simply .replace() on the html. If you really want to use the solution using a module, then you can do it there by concatinating the string like usual html = "<html>"+yourinsertedvariable+"...</html>
Ok, well the idea is we make another file .html and call it in say myPiClass.py class but all the variables of .myPiClass.py class is not accessible in .html file.Sorry I did'nt get your above answer?
Well first you must decide what you want to do. Do you want to put all your html code into a variable? This is not a good practice. If you want to do this anyway use the way I describe in the first code block.
Then you can insert variables there, for example by declaring a function insertMyVariables(var1, var2) which sets and returns the html-string: html = "<h1>"+var1+"<h1> ... return html" As I said I really don't think you should do it this way. If you want to do things cleaner you put your html-code into a seperate html file, read it, use .replace() on the read string and use it to render your pdf.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.