From 6e6f9fbd0d696e35b798f850e24781dbe32774cd Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= <fpeters@entrouvert.com>
Date: Wed, 21 Aug 2019 10:03:45 +0200
Subject: [PATCH] misc: remove possibility to download PDF of statistics
 (#35281)

---
 data/print-html-as-pdf.js    | 31 -------------------------------
 setup.py                     |  2 +-
 wcs/backoffice/management.py |  9 ---------
 wcs/qommon/misc.py           | 22 ----------------------
 4 files changed, 1 insertion(+), 63 deletions(-)
 delete mode 100644 data/print-html-as-pdf.js

diff --git a/data/print-html-as-pdf.js b/data/print-html-as-pdf.js
deleted file mode 100644
index 5808eae5e..000000000
--- a/data/print-html-as-pdf.js
+++ /dev/null
@@ -1,31 +0,0 @@
-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();
-    $('#sidepage, #main').removeClass('sidepage-expanded');
-    $('html').css('background', 'white').css('font-size', '8pt');
-    $('div#main').css('width', '95%');
-    $('div#main-content').css('width', '95%').css('border', 'none');
-    $('#statistics .criterias').css('display', 'block');
-    $('div#main-content > h2').css({
-            'border-bottom': '1px solid #666',
-            'font-weight': 'bold'});
-    var date = $('<p>' + new Date().toLocaleString() + '</p>').appendTo('div#main-content');
-    date.css({
-            'position': 'absolute',
-            'top': '2ex',
-            'right': '1em'});
-  });
-  page.render(system.args[1] + '.pdf');
-  phantom.exit();
-});
diff --git a/setup.py b/setup.py
index 3c9e50132..b40df64fd 100644
--- a/setup.py
+++ b/setup.py
@@ -122,7 +122,7 @@ setup(
                 data_tree('share/wcs/themes/', 'data/themes/') + \
                 data_tree('share/wcs/vendor/', 'data/vendor/') + \
                 data_tree('share/wcs/qommon/', 'wcs/qommon/static/') +
-                [('share/wcs/', ['data/webbots', 'data/print-html-as-pdf.js'])]
+                [('share/wcs/', ['data/webbots'])]
     )
 
 if local_cfg:
diff --git a/wcs/backoffice/management.py b/wcs/backoffice/management.py
index 28f1fb9e0..b71b6b5c2 100644
--- a/wcs/backoffice/management.py
+++ b/wcs/backoffice/management.py
@@ -1790,8 +1790,6 @@ 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()
 
@@ -1895,13 +1893,6 @@ class FormPage(Directory):
         page += htmltext('<h2>%s - %s</h2>') % (self.formdef.name, _('Statistics'))
         page += htmltext(r)
         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, excluded_fields=None):
diff --git a/wcs/qommon/misc.py b/wcs/qommon/misc.py
index 7727d3877..f5d5b1d73 100644
--- a/wcs/qommon/misc.py
+++ b/wcs/qommon/misc.py
@@ -515,28 +515,6 @@ def json_loads(value, charset=None):
     charset = (get_publisher() and get_publisher().site_charset) or 'utf-8'
     return json_encode_helper(json.loads(value), charset)
 
-def can_decorate_as_pdf():
-    return os.path.exists('/usr/bin/phantomjs')
-
-def decorate_as_pdf(content):
-    from qommon import template
-    html_page = get_publisher().render_response(content)
-    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', '--ssl-protocol=any', 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
-
 def json_response(data):
     get_response().set_content_type('application/json')
     if get_request().get_environ('HTTP_ORIGIN'):
-- 
2.23.0

