backoffice: add printing of stats page to pdf (#6896)
This commit is contained in:
parent
8db0534f6f
commit
39896c1869
|
@ -0,0 +1,20 @@
|
|||
var page = require('webpage').create();
|
||||
var system = require('system');
|
||||
|
||||
page.paperSize = {
|
||||
width: '29cm',
|
||||
height: '21cm',
|
||||
margin: {
|
||||
top: '1cm', left: '1cm', right: '1cm', bottom: '1cm'
|
||||
}
|
||||
};
|
||||
|
||||
page.open(system.args[1], function() {
|
||||
x = page.evaluate(function() {
|
||||
$('#top, #header, #sidebar, #sidebar-toggle, #breadcrumb, a.back').hide();
|
||||
$('html').css('background', 'white').css('font-size', '8pt');
|
||||
$('div#main-content').css('width', '95%').css('border', 'none');
|
||||
});
|
||||
page.render(system.args[1] + '.pdf');
|
||||
phantom.exit();
|
||||
});
|
|
@ -1097,6 +1097,8 @@ class FormPage(Directory):
|
|||
r += htmltext('<form id="listing-settings" action="stats">')
|
||||
r += self.get_filter_sidebar(selected_filter=selected_filter, mode='stats')
|
||||
r += htmltext('<button class="refresh">%s</button>') % _('Refresh')
|
||||
if misc.can_decorate_as_pdf():
|
||||
r += htmltext('<button class="pdf">%s</button>') % _('Download PDF')
|
||||
r += htmltext('</form>')
|
||||
return r.getvalue()
|
||||
|
||||
|
@ -1159,7 +1161,14 @@ class FormPage(Directory):
|
|||
page = TemplateIO(html=True)
|
||||
page += htmltext('<h2>%s - %s</h2>') % (self.formdef.name, _('Statistics'))
|
||||
page += htmltext(r)
|
||||
page += htmltext('<a href=".">%s</a>') % _('Back')
|
||||
page += htmltext('<a class="back" href=".">%s</a>') % _('Back')
|
||||
|
||||
if 'pdf' in get_request().form:
|
||||
pdf_content = misc.decorate_as_pdf(page.getvalue())
|
||||
response = get_response()
|
||||
response.set_content_type('application/pdf')
|
||||
return pdf_content
|
||||
|
||||
return page.getvalue()
|
||||
|
||||
def stats_fields(self, values):
|
||||
|
|
|
@ -23,9 +23,11 @@ import urllib
|
|||
import socket
|
||||
import base64
|
||||
import json
|
||||
import subprocess
|
||||
import tempfile
|
||||
import unicodedata
|
||||
|
||||
from quixote import get_publisher, get_session
|
||||
from quixote import get_publisher, get_session, get_response
|
||||
from quixote.html import htmltext
|
||||
|
||||
from qommon import get_cfg, get_logger, ezt
|
||||
|
@ -391,3 +393,25 @@ class JSONEncoder(json.JSONEncoder):
|
|||
return datetime.datetime.utcfromtimestamp(time.mktime(obj)).isoformat() + 'Z'
|
||||
# Let the base class default method raise the TypeError
|
||||
return json.JSONEncoder.default(self, obj)
|
||||
|
||||
def can_decorate_as_pdf():
|
||||
return os.path.exists('/usr/bin/phantomjs')
|
||||
|
||||
def decorate_as_pdf(content):
|
||||
from qommon import template
|
||||
html_page = template.decorate(content, get_response())
|
||||
html_page = html_page.replace('<head>',
|
||||
'<head><base href="%s"><meta charset="%s">' %
|
||||
(get_publisher().get_frontoffice_url(),
|
||||
get_publisher().site_charset))
|
||||
tmpfile = tempfile.NamedTemporaryFile(suffix='.html', delete=False)
|
||||
tmpfile.write(html_page)
|
||||
tmpfile.close()
|
||||
phantomjs_script = os.path.join(get_publisher().DATA_DIR, 'print-html-as-pdf.js')
|
||||
subprocess.check_call(['phantomjs', phantomjs_script, tmpfile.name])
|
||||
pdf_fd = open(tmpfile.name + '.pdf')
|
||||
pdf_content = pdf_fd.read()
|
||||
pdf_fd.close()
|
||||
os.unlink(tmpfile.name + '.pdf')
|
||||
os.unlink(tmpfile.name)
|
||||
return pdf_content
|
||||
|
|
|
@ -150,6 +150,15 @@ $(function() {
|
|||
return false;
|
||||
});
|
||||
|
||||
$('button.pdf').click(function() {
|
||||
if (window.location.pathname.indexOf('?') == -1) {
|
||||
window.location = window.location + '?pdf=on';
|
||||
} else {
|
||||
window.location = window.location + '&pdf=on';
|
||||
}
|
||||
return false;
|
||||
});
|
||||
|
||||
/* possibility to toggle the sidebar */
|
||||
$('#main-content').after($('<span id="sidebar-toggle">⁞</span>'));
|
||||
$('#sidebar-toggle').click(function() {
|
||||
|
|
Loading…
Reference in New Issue