Pergunta

I need a code snippet to generate a PDF file from a HTML page that can contain images and some CSS.

I'm using Google AppEngine with Python 2.7 and webapp2 (not Django).

I know this question may deserve a down-vote because it appears I didn't research enough before asking, but I actually did and couldn't find a specific example on how to accomplish this.

Foi útil?

Solução

GAE provides the conversion API, which has the capability to convert html to PDF (among some others). For an example on how to convert an html with images to PDF see the answer to the question Incomplete Google App Engine Documentation on Conversion API. If you need to include a CSS, then you need to include it in the html.

Outras dicas

There is no native support from python in converting html to pdf. However, you might want to search for library that someone wrote that does that functionality. See http://www.xhtml2pdf.com/ for more info.

After you manage to write pdf data into a string, simply by setting pdf headers with webapp2 you can serve pdf file to client browser.

# In request handler 
self.response.headers['Content-Type'] = 'application/pdf'
self.response.headers['Content-Disposition'] = 'attachment;filename=downloaded.pdf'
self.response.write(pdfdata)

I made xhtml2pdf to work on Google AppEngine Python.

Just send your html string as a (get/post) to this url and it will give you the pdf.

USING HTTP POST: Post html string in html parameter to the following url. http://metrocdn.appspot.com/pdf/what_ever_filename_you_want.pdf

OR USING HTTP GET: http://metrocdn.appspot.com/pdf/what_ever_filename_you_want.pdf?html=your-html-string

Regards

Sarfaraz Farooqui

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top