Implemented PDF options

This commit is contained in:
benweet 2014-08-14 00:44:33 +01:00
parent ae8adddf5d
commit 3f8b5a865a
10 changed files with 449 additions and 372 deletions

View File

@ -18,10 +18,10 @@ module.exports = function(req, res, next) {
}
// Margins
var marginRight = parseInt(options.marginRight);
params.push('-R', isNaN(marginRight) ? 25 : marginRight);
var marginTop = parseInt(options.marginTop);
params.push('-T', isNaN(marginTop) ? 25 : marginTop);
var marginRight = parseInt(options.marginRight);
params.push('-R', isNaN(marginRight) ? 25 : marginRight);
var marginBottom = parseInt(options.marginBottom);
params.push('-B', isNaN(marginBottom) ? 25 : marginBottom);
var marginLeft = parseInt(options.marginLeft);
@ -42,8 +42,7 @@ module.exports = function(req, res, next) {
options.footerFontSize && params.push('--footer-font-size ', options.footerFontSize);
// Page size
var pageSize = options.pageSize;
params.push('--page-size', authorizedPageSizes.indexOf(pageSize) === -1 ? 'A4' : pageSize);
params.push('--page-size', authorizedPageSizes.indexOf(options.pageSize) === -1 ? 'A4' : options.pageSize);
// wkhtmltopdf can't access /dev/stdout on Amazon EC2 for some reason
var filePath = '/tmp/' + Date.now() + '.pdf';

View File

@ -14,9 +14,10 @@ define([
"text!html/bodyIndex.html",
"text!html/bodyViewer.html",
"text!html/tooltipSettingsTemplate.html",
"text!html/tooltipSettingsPdfOptions.html",
"storage",
'pagedown'
], function($, _, crel, editor, layout, constants, utils, storage, settings, eventMgr, MonetizeJS, bodyIndexHTML, bodyViewerHTML, settingsTemplateTooltipHTML) {
], function($, _, crel, editor, layout, constants, utils, storage, settings, eventMgr, MonetizeJS, bodyIndexHTML, bodyViewerHTML, settingsTemplateTooltipHTML, settingsPdfOptionsTooltipHTML) {
var core = {};
@ -142,8 +143,8 @@ define([
utils.setInputValue("#textarea-settings-publish-template", settings.template);
// PDF template
utils.setInputValue("#textarea-settings-pdf-template", settings.pdfTemplate);
// PDF page size
utils.setInputValue("#input-settings-pdf-page-size", settings.pdfPageSize);
// PDF options
utils.setInputValue("#textarea-settings-pdf-options", settings.pdfOptions);
// SSH proxy
utils.setInputValue("#input-settings-ssh-proxy", settings.sshProxy);
@ -189,8 +190,8 @@ define([
newSettings.template = utils.getInputTextValue("#textarea-settings-publish-template", event);
// PDF template
newSettings.pdfTemplate = utils.getInputTextValue("#textarea-settings-pdf-template", event);
// PDF page size
newSettings.pdfPageSize = utils.getInputValue("#input-settings-pdf-page-size");
// PDF options
newSettings.pdfOptions = utils.getInputJSONValue("#textarea-settings-pdf-options", event);
// SSH proxy
newSettings.sshProxy = utils.checkUrl(utils.getInputTextValue("#input-settings-ssh-proxy", event), true);
@ -331,10 +332,12 @@ define([
});
var $alerts = $();
function hasPaid(payments) {
return payments && (
function isSponsor(payments) {
var result = payments && (
(payments.chargeOption && payments.chargeOption.alias == 'once') ||
(payments.subscriptionOption && payments.subscriptionOption.alias == 'yearly'));
eventMgr.isSponsor = result;
return result;
}
function removeAlerts() {
@ -346,11 +349,10 @@ define([
monetize.getPayments({
pricingOptions: [
'once',
'monthly',
'yearly'
]
}, function(err, payments) {
if(hasPaid(payments)) {
if(isSponsor(payments)) {
eventMgr.onMessage('Thank you for sponsoring StackEdit!');
removeAlerts();
}
@ -363,7 +365,7 @@ define([
}
monetize.getPaymentsImmediate(function(err, payments) {
removeAlerts();
if(!hasPaid(payments)) {
if(!isSponsor(payments)) {
_.each(document.querySelectorAll('.modal-body'), function(modalBodyElt) {
var $elt = $('<div class="alert alert-danger">Please consider <a href="#">sponsoring StackEdit</a> for $5/year (or <a href="#">sign in</a> if you\'re already a sponsor).</div>');
$elt.find('a').click(performPayment);
@ -542,6 +544,7 @@ define([
'<b class="text-danger">NOTE: Backlinks in Stack Exchange Q/A are not welcome.</b>'
].join(''));
utils.createTooltip(".tooltip-template", settingsTemplateTooltipHTML);
utils.createTooltip(".tooltip-pdf-options", settingsPdfOptionsTooltipHTML);
// Avoid dropdown panels to close on click
$("div.dropdown-menu").click(function(e) {

View File

@ -10,6 +10,11 @@ define([
var dialogAbout = new Extension("dialogAbout", 'Dialog "About"');
var eventMgr;
dialogAbout.onEventMgrCreated = function(eventMgrParameter) {
eventMgr = eventMgrParameter;
};
var monetize = new MonetizeJS({
applicationID: 'iklMbzDI7dvMEScb'
});
@ -22,6 +27,7 @@ define([
monetize.getPayments({
summary: true
}, function() {
eventMgr.onMessage('Please refresh the page for your sponsorship to take effect.');
});
});
};

View File

@ -89,7 +89,7 @@
<a href="#" data-toggle="collapse" data-target=".collapse-synchronize"
class="list-group-item">
<div><i class="icon-refresh"></i> Synchronize</div>
<small>Backup, collaborate in the cloud</small>
<small>Backup, collaborate...</small>
</a>
<div class="sub-menu collapse collapse-synchronize clearfix">
<ul class="nav alert alert-danger show-already-synchronized">
@ -159,7 +159,7 @@
<li><a class="action-download-template" href="#"><i
class="icon-download"></i> Using template</a></li>
<li><a class="action-download-pdf" href="#"><i
class="icon-download"></i> As PDF</a></li>
class="icon-download"></i> As PDF <sup class="text-danger">sponsor</sup></a></li>
</ul>
</div>
@ -1072,14 +1072,12 @@
</div>
</div>
<div class="form-group">
<label class="col-sm-4 control-label" for="input-settings-pdf-page-size">PDF page size</label>
<label class="col-sm-4 control-label" for="textarea-settings-pdf-options">PDF options
<a href="#" class="tooltip-pdf-options">(?)</a>
</label>
<div class="col-sm-7">
<select id="input-settings-pdf-page-size" class="form-control">
<option value="A3">A3</option>
<option value="A4">A4</option>
<option value="Legal">Legal</option>
<option value="Letter">Letter</option>
</select>
<textarea id="textarea-settings-pdf-options"
class="form-control"></textarea>
</div>
</div>
<div class="form-group">
@ -1303,4 +1301,24 @@
</div>
<div class="modal fade modal-sponsorship-required">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h2 class="modal-title">Sponsorship required</h2>
</div>
<div class="modal-body">
<p>This feature is restricted to sponsors only as it's a web service hosted on Amazon EC2.
Note that sponsoring StackEdit would cost you only <b>$5/year</b>...</p>
</div>
<div class="modal-footer">
<a href="#" class="btn btn-primary"
data-dismiss="modal">Close</a>
</div>
</div>
</div>
</div>
<div id="dropboxjs" data-app-key="x0k2l8puemfvg0o"></div>

View File

@ -0,0 +1,19 @@
Option object in JSON format. Possible attributes:
<br><br>
<b>marginTop</b>,
<b>marginRight</b>,
<b>marginBottom</b>,
<b>marginLeft</b>,<br>
<b>headerCenter</b>,
<b>headerLeft</b>,
<b>headerRight</b>,
<b>headerFontName</b>,
<b>headerFontSize</b>,<br>
<b>footerCenter</b>,
<b>footerLeft</b>,
<b>footerRight</b>,
<b>footerFontName</b>,
<b>footerFontSize</b><br>
<b>pageSize</b><br><br>
Please check out the
<a target="_blank" href="http://wkhtmltopdf.org/usage/wkhtmltopdf.txt">wkhtmltopdf documentation</a> for more info.

View File

@ -86,7 +86,7 @@ define([
var syncIndex = createSyncIndex(path);
var fileDesc = fileMgr.getFileFromSyncIndex(syncIndex);
if(fileDesc !== undefined) {
return eventMgr.onError('"' + fileDesc.title + '" was already imported.');
return eventMgr.onError('"' + fileDesc.title + '" is already in your local documents.');
}
importPaths.push(path);
});

View File

@ -91,7 +91,7 @@ define([
var syncIndex = createSyncIndex(doc.id);
var fileDesc = fileMgr.getFileFromSyncIndex(syncIndex);
if(fileDesc !== undefined) {
return eventMgr.onError('"' + fileDesc.title + '" was already imported.');
return eventMgr.onError('"' + fileDesc.title + '" is already in your local documents.');
}
importIds.push(doc.id);
});

View File

@ -9,6 +9,7 @@ define([
"fileSystem",
"fileMgr",
"sharing",
"monetizejs",
"classes/Provider",
"classes/AsyncTask",
"providers/bloggerProvider",
@ -22,7 +23,7 @@ define([
"providers/sshProvider",
"providers/tumblrProvider",
"providers/wordpressProvider"
], function($, _, constants, utils, storage, settings, eventMgr, fileSystem, fileMgr, sharing, Provider, AsyncTask) {
], function($, _, constants, utils, storage, settings, eventMgr, fileSystem, fileMgr, sharing, MonetizeJS, Provider, AsyncTask) {
var publisher = {};
@ -114,6 +115,7 @@ define([
var publishAttributesList = [];
var publishFileDesc;
var publishHTML;
function publishLocation(callback, errorFlag) {
// No more publish location for this document
@ -187,7 +189,7 @@ define([
var publishIndex;
do {
publishIndex = "publish." + utils.randomString();
} while (_.has(storage, publishIndex));
} while(_.has(storage, publishIndex));
publishAttributes.publishIndex = publishIndex;
fileDesc.addPublishLocation(publishAttributes);
eventMgr.onNewPublishSuccess(fileDesc, publishAttributes);
@ -195,6 +197,7 @@ define([
// Initialize the "New publication" dialog
var newLocationProvider;
function initNewLocation(provider) {
var defaultPublishFormat = provider.defaultPublishFormat || "markdown";
newLocationProvider = provider;
@ -332,21 +335,33 @@ define([
var content = publisher.applyTemplate(fileDesc, undefined, currentHTML);
utils.saveAs(content, fileDesc.title + (settings.template.indexOf("documentHTML") === -1 ? ".md" : ".html"));
});
var monetize = new MonetizeJS({
applicationID: 'iklMbzDI7dvMEScb'
});
$(".action-download-pdf").click(function() {
var fileDesc = fileMgr.currentFile;
var content = publisher.applyTemplate(fileDesc, {
customTmpl: settings.pdfTemplate
}, currentHTML);
var task = new AsyncTask();
var pdf;
var pdf, token;
task.onRun(function() {
if(isOffline === true) {
eventMgr.onError("Operation not available in offline mode.");
task.chain();
return;
return task.chain();
}
if(!eventMgr.isSponsor) {
$('.modal-sponsorship-required').modal('show');
return task.chain();
}
monetize.getTokenImmediate(function(err, result) {
token = result || '';
task.chain();
});
});
task.onRun(function() {
var xhr = new XMLHttpRequest();
xhr.open('POST', constants.HTMLTOPDF_URL, true);
xhr.open('POST', constants.HTMLTOPDF_URL + '?token=' + encodeURIComponent(token) + '&options=' + encodeURIComponent(settings.pdfOptions), true);
xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xhr.responseType = 'blob';
xhr.onreadystatechange = function() {

View File

@ -52,10 +52,10 @@ define([
constants.MAIN_URL,
'libs/MathJax/MathJax.js?config=TeX-AMS_HTML"></script>\n',
'</head>\n',
'<body class="pdf"><%= documentHTML %></body>\n',
'<body><%= documentHTML %></body>\n',
'</html>'
].join(""),
pdfPageSize: 'A4',
pdfOptions: '{}',
sshProxy: constants.SSH_PROXY_URL,
extensionSettings: {}
};

View File

@ -129,7 +129,7 @@ define([
// For input control
function inputError(element, event) {
if(event !== undefined) {
element.stop(true, true).addClass("error").delay(1000).queue(function() {
element.stop(true, true).addClass("error").delay(3000).queue(function() {
$(this).removeClass("error");
$(this).dequeue();
});
@ -223,6 +223,23 @@ define([
return value;
};
// Return input value and check that it's a valid JSON
utils.getInputJSONValue = function(element, event) {
element = jqElt(element);
var value = utils.getInputTextValue(element, event);
if(value === undefined) {
return undefined;
}
try {
JSON.parse(value);
}
catch(e) {
inputError(element, event);
return undefined;
}
return value;
};
// Return checkbox boolean value
utils.getInputChecked = function(element) {
element = jqElt(element);