Html to pdf - jordy33/turbogears_tutorial GitHub Wiki
Click [here] for additional configuration info
Install Xvfd:
$ sudo apt-get install xvfb
Install Fonts:
$ sudo apt-get install xfonts-100dpi xfonts-75dpi xfonts-scalable xfonts-cyrillic
Install flashplugin:
$ sudo apt-get install flashplugin-nonfree
Install wkhtmltopdf:
cd /tmp
wget https://downloads.wkhtmltopdf.org/0.12/0.12.5/wkhtmltox_0.12.5-1.xenial_amd64.deb
dpkg -i wkhtmltox_0.12.5-1.xenial_amd64.deb
Modify setup.py in Turbogears
Add to install requires
"pdfkit"
STEP 1
Create utility.py in lib directory
import regex
from pythonmercuryms.lib.helpers import whoami
import os,sys
from tg import abort
from pathlib import Path
import pdfkit
from tg import render_template
class ExportPDF():
"""
Export CVS
"""
@classmethod
def create(cls, parameters,name,url):
cwd=os.getenv('MERCURY_DIR')
np=cwd.rfind("/")+1
who=whoami()
if who=="":
error="PDF Failure"
reason="User not logged"
message = "The following {} occured, this is due to {}, please DEBUG the url : {}".format(error, reason,url)
abort(status_code=500, detail=message)
file_name=cwd+"/"+cwd[np:].replace(".","")+"/public/"+who+"_"+name+".pdf"
my_file = Path(file_name)
if my_file.is_file():
os.remove(file_name)
## PDF Generation ##
options = {
'page-width': '11.0in',
'page-height': '8.5in',
'margin-top': '0.1in',
'margin-right': '0.1in',
'margin-bottom': '0.1in',
'margin-left': '0.1in',
'encoding': "UTF-8",
'quiet': '',
}
body = render_template(parameters, "mako", url)
pdfkit.from_string(body, file_name, options=options)
return "/"+who+"_"+name+".pdf"
STEP 2
Add the following to test controller
At top:
from tg import expose, render_template
from pythonmercuryms.lib.utililty import ExportPDF
At bottom:
@expose('pythonmercuryms.templates.test.pdf')
def pdf(self):
list=[]
file_name=ExportPDF.create({"parameter1": 'Jorge'},"pdftest","pythonmercuryms.templates.test.pdfexample")
print("fn:{}".format(file_name))
return dict(file_name=file_name)
STEP 3
Create pdf.mak in templates tests
<style type="text/css">
</style>
<script type="text/javascript">
</script>
<div class="row">
<div class="col-md-12">
<div class="page-header">
<h2>PDF Example</h2>
</div>
<div class="container" align="left">
<a href='${h.url()}/${file_name}' download>
<img border="0" src="${tg.url('/img/download.png')}" alt="Press here to Download" width="104" height="142">
</a>
</form>
</div>
</div>
</div>
STEP 4
Create pdfExample.mak in templates tests with the following
<p>Hello</p>
<p>${parameter1}</p>
STEP 5:
Add to master.mak the following
<button onclick="openTab('${_('pdf')}','${h.url()}/test/pdf/')" title="PDF">${_('pdf')}</button>
PDF needs utf-8 if is encoded in other we must translate:
Decode in Python 3 Decode byte array in Python 3 (Python Shell 3.7.2):
>>> bytes([0xE0]).decode('iso-8859-1')
'à'
>>> b'\xe0'.decode('iso-8859-1')
'à'
>>> bytes([0xC3, 0xA0]).decode('utf-8')
'à'
>>> b'\xc3\xa0'.decode('utf-8')
'à'
>>> b'\xc3\xa0'.decode()
'à'
Example From iso-8859-1 to utf8:
a='Solución útil y apañada a UTF-8'
b = bytes(a, 'iso-8859-1')
c=b.decode('utf-8')
print(c)
The result in c:
Solución útil y apañada a UTF-8