diff --git a/controllers/plugin_dbui.py b/controllers/plugin_dbui.py index 0adcd8d0fabbef6201de99d2e862d98a1924bab8..ee2ad06611ac6fe73888c1017b7076f167ce8c31 100644 --- a/controllers/plugin_dbui.py +++ b/controllers/plugin_dbui.py @@ -18,6 +18,7 @@ API = """ App.csvUrl = '/%s/plugin_dbui/csv'; App.config = %s; App.debug = %s; +App.latex2pdfUrl = '/%s/plugin_dbui/latex2pdf.pdf'; App.name = '%s'; App.REMOTE_API = { 'url': '/%s/plugin_dbui/call', @@ -149,6 +150,7 @@ def dbui_conf(): json.dumps(config), debug, app, + app, app, json.dumps(di)) @@ -360,43 +362,44 @@ def index(): def latex2pdf(): - """Convert a LaTeX file into PDF. + """Convert a LaTeX string into PDF. + It is mandatory to define the request argument 'latex'. """ from subprocess import call from tempfile import TemporaryFile from uuid import uuid4 - latex = request.vars.latex -# -# # create the latex file in the private directory -# cwd = os.getcwd() -# os.chdir(os.path.join(request.folder, 'private')) -# -# fn = str(uuid4()) -# fi = open('%s.tex' % fn, 'wb') -# fi.write(latex) -# fi.close() -# -# # convert the latex file into pdf -# cmd = ['pdflatex', '-interaction', 'nonstopmode', '%s.tex' % fn] -# call(cmd, stdout=TemporaryFile()) -# -# # copy the pdf in a string -# fi = open('%s.pdf' % fn, 'rb') -# s_pdf = fi.read() -# fi.close() -# -# # clean latex processing -# for ext in ('aux', 'log', 'pdf', 'tex'): -# f = '%s.%s' % (fn, ext) -# if os.path.exists(f): -# os.remove(f) -# -# # go back to the web2py main directory -# os.chdir(cwd) - s_pdf = latex - return dict(data=s_pdf) + latex_string = request.vars.latex + + # create the latex file in the private directory + cwd = os.getcwd() + os.chdir(os.path.join(request.folder, 'private')) + + fn = str(uuid4()) + fi = open('%s.tex' % fn, 'wb') + fi.write(latex_string) + fi.close() + + # convert the latex file into pdf + cmd = ['pdflatex', '-interaction', 'nonstopmode', '%s.tex' % fn] + call(cmd, stdout=TemporaryFile()) + + # copy the pdf file into a string + fi = open('%s.pdf' % fn, 'rb') + pdf_string = fi.read() + fi.close() + + # clean latex processing + for ext in ('aux', 'log', 'pdf', 'tex'): + f = '%s.%s' % (fn, ext) + if os.path.exists(f): + os.remove(f) + + # go back to the web2py main directory + os.chdir(cwd) + + return dict(data=pdf_string) def status(): diff --git a/static/plugin_dbui/src/App.js b/static/plugin_dbui/src/App.js index af7d3a0e0c3f388d728be6be632d97ec03099991..92f3fbe5be7505383f30072955a2a38d21ae3aa0 100644 --- a/static/plugin_dbui/src/App.js +++ b/static/plugin_dbui/src/App.js @@ -57,6 +57,13 @@ Ext.define('App', { */ debug: false, + /** + * @property {String} + * The URL of the controller to convert latex string into PDF file, + * namely /application/plugin_dbui/latex2pdf.pdf. + */ + latex2pdfUrl: undefined, + /** * The configuration of the Ext.Direct service. * It contains the definition of the remote procedure. diff --git a/static/plugin_dbui/src/grid/plugin/Export.js b/static/plugin_dbui/src/grid/plugin/Export.js index 5cbc3cd80b98f07642437ddd218fe299a2ac1093..965bb855c85784071651011033b984d40ac7d2e5 100644 --- a/static/plugin_dbui/src/grid/plugin/Export.js +++ b/static/plugin_dbui/src/grid/plugin/Export.js @@ -111,43 +111,6 @@ Ext.define('App.grid.plugin.Export', { return value; }, - /** - * Download the data as a file using an URI. - * The URI is built from the arguments and the data are HTML - * encoded to preserve special character like quote, /, ?, ... - * - * @param {String} - * The mine type of the return file, e.g "text/csv". - * - * @param {String} - * The data encoding, e.g. "utf-8". - * - * @param {String/String[]} - * The content of the file, one string per line. - * - */ - downloadViaURI: function (minetype, encoding, data) { - - "use strict"; - - var uri; - - // build the data string in which special - // character like quote, / , ? are HTML encoded - if (Ext.isArray(data)) { - data = data.join(this.newLine); - } - data = encodeURIComponent(data); - - // build the URI - uri = "data:" + minetype + ";"; - uri += "charset=" + encoding +","; - uri += data; - - // stimulate the browser to mimic the field download - window.location.href = uri; - }, - /** * Get the content of the cells as rendered by the grid. * The method handle the grouping cell as well. @@ -364,7 +327,7 @@ Ext.define('App.grid.plugin.Export', { "use strict"; var data = this.getCSV(); - this.downloadViaURI("text/csv", "utf-8", data); + this.save("text/csv", "utf-8", data); }, /** @@ -410,70 +373,26 @@ Ext.define('App.grid.plugin.Export', { "use strict"; var data = this.getLaTeX(); - this.downloadViaURI("application/x-latex", "utf-8", data); + this.save("application/x-latex", "utf-8", data); }, /** * Export the content of the grid as a PDF file. + * The latex file is generated on the client side and transfer to + * the server where the conversion take place. * */ - downloadViaIFRAME: function (panel, url) { - - "use strict"; - - var frameId = 'mainPanelIframe', - iframe; - - // destroy IFRAME previously embedded in the panel - iframe = Ext.getBody().getById(frameId); - if (iframe) { - Ext.destroy(iframe); - } - - // launch a progress bar - Ext.MessageBox.show({ - msg: 'Preparing your file, please wait...', - progressText: 'Saving...', - width: 300, - wait: true - }); - - // append the IFRAME to the panel therefore - // the browser will launch the downloading. - // - // NOTE: the IFRAME cover almost the full space. - // this is useful when the PDF is embedded in the panel. - // - // NOTE: keep the background colour to white - // - iframe = Ext.getBody().createChild({ - tag: 'iframe', - id: frameId, - frameborder: 0, - width: '100%', - height: '99%', - style: 'background: #00FF00;', - src: url - }); - - // when the download is finish hide message box - iframe.on('load', function () { - Ext.MessageBox.hide(); - }, this, {single: true}); - }, - onPdfExport: function () { "use strict"; - /* var data, - form, - url; + form; data = this.getLaTeX().join(this.newLine); - url = '/plugin_dbui/plugin_dbui/latex2pdf.html'; + // send the latex data to the server. + // the method used standardSubmit using a form. form = Ext.create('Ext.form.Panel', { items: [{ name: 'latex', @@ -481,35 +400,49 @@ Ext.define('App.grid.plugin.Export', { xtype:'textfield' }], standardSubmit: true, - url: url + url: App.latex2pdfUrl }); - console.log(form); form.getForm().submit(); - */ + }, + + /** + * Save the data as a file using an URI. + * The URI is built from the arguments and the data are HTML + * encoded to preserve special character like quote, /, ?, ... + * + * @param {String} + * The mine type of the return file, e.g "text/csv". + * + * @param {String} + * The data encoding, e.g. "utf-8". + * + * @param {String/String[]} + * The content of the file. Either a string of a list of string, + * one string per line. + * + */ + save: function (minetype, encoding, data) { - var url, - vars = {latex: undefined}; + "use strict"; - // build the url - vars.latex = this.getLaTeX().join(this.newLine); + var uri; - url = '/plugin_dbui/plugin_dbui/latex2pdf.html' + '?' + Ext.Object.toQueryString(vars); + // build the data string in which special + // character like quote, / , ? are HTML encoded + if (Ext.isArray(data)) { + data = data.join(this.newLine); + } + data = encodeURIComponent(data); - this.downloadViaIFRAME(this.getCmp(), url); + // build the URI + uri = "data:" + minetype + ";"; + uri += "charset=" + encoding +","; + uri += data; - /* - // trigger the latex to pdf conversion on the server side - Ext.Ajax.request({ - url: '/plugin_dbui/plugin_dbui/latex2pdf.html', - params: {latex: data}, - scope: this, - success: function (response) { - console.log(response.responseText); - this.download("application/pdf", "utf-8", response.responseText, "my_data.pdf"); - } - }); */ + // stimulate the browser to mimic the field download + window.location.href = uri; + }, - } }); \ No newline at end of file diff --git a/static/plugin_dbui/src/panel/WithUrlSelector.js b/static/plugin_dbui/src/panel/WithUrlSelector.js index 048c792fa742115f39a54a39cffbb680e4539874..9f43045f91d9012664e754fa8eb9a6b07a5e57c5 100644 --- a/static/plugin_dbui/src/panel/WithUrlSelector.js +++ b/static/plugin_dbui/src/panel/WithUrlSelector.js @@ -238,7 +238,7 @@ Ext.define('App.panel.WithUrlSelector', { // // NOTE: keep the background colour to white // - iframe = Ext.DomHelper.insertFirst(this.mainPanel.getEl(), { + iframe = this.mainPanel.getEl().createChild({ tag: 'iframe', id: 'mainPanelIframe', frameborder: 0, @@ -249,7 +249,6 @@ Ext.define('App.panel.WithUrlSelector', { }); // when the download is finish hide message box - iframe = this.mainPanel.getEl().getById('mainPanelIframe'); iframe.on('load', function () { Ext.MessageBox.hide(); }, this, {single: true}); diff --git a/views/plugin_dbui/latex2pdf.html b/views/plugin_dbui/latex2pdf.pdf similarity index 98% rename from views/plugin_dbui/latex2pdf.html rename to views/plugin_dbui/latex2pdf.pdf index ec25db31f3cf0872504d632d6a66b9721f9aa09f..b2782698cd99e3e7318825882ec95344a3aeeff6 100644 --- a/views/plugin_dbui/latex2pdf.html +++ b/views/plugin_dbui/latex2pdf.pdf @@ -3,7 +3,7 @@ try: response.headers['Content-Type']='application/pdf' response.write(data, escape=False) - + except: raise HTML(405, T('LaTeX to PDF conversion failed.'))