Skip to content
Snippets Groups Projects
Commit bfb59038 authored by LE GAC Renaud's avatar LE GAC Renaud
Browse files

The latex to pdf conversion is working with the grid plugin Export.

parent 7566c763
No related branches found
No related tags found
No related merge requests found
......@@ -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():
......
......@@ -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.
......
......@@ -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
......@@ -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});
......
......@@ -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.'))
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment