export pdf local ok

This commit is contained in:
life
2015-12-21 23:25:36 +08:00
parent 743e752d08
commit 73584c8ff1
10 changed files with 1907 additions and 3 deletions

10
main.js
View File

@@ -1,6 +1,7 @@
var app = require('app'); // Module to control application life.
var BrowserWindow = require('browser-window'); // Module to create native browser window.
var ipc = require('ipc');
var ipc = require('ipc');
var pdfMain = require('pdf_main');
// Report crashes to our server.
require('crash-reporter').start();
@@ -87,6 +88,7 @@ function openIt() {
var leanoteProtocol = require('leanote_protocol');
leanoteProtocol.init();
// Create the browser window.
mainWindow = new BrowserWindow({
width: 1050,
@@ -96,6 +98,8 @@ function openIt() {
}
);
console.log('load: file://' + __dirname + '/note.html');
// and load the index.html of the app.
mainWindow.loadUrl('file://' + __dirname + '/note.html');
@@ -123,11 +127,13 @@ function openIt() {
e.preventDefault();
mainWindow.webContents.send('closeWindow');
});
// 前端发来可以关闭了
ipc.on('quit-app', function(event, arg) {
console.log('get quit-app request');
mainWindow.destroy();
mainWindow = null;
});
pdfMain.init();
}

17
node_modules/leanote_protocol.js generated vendored
View File

@@ -25,7 +25,21 @@ var leanoteProtocol = {
callback();
}
});
}
else {
// js, 请求, 导出pdf
// leanote://public/a.js
console.log(url);
var prefix = 'leanote://public';
// leanote://public/libs/MathJax/MathJax.js?config=TeX-AMS_HTML Failed to load resource: net::ERR_FILE_NOT_FOUND
if (url.substr(0, prefix.length) == prefix) {
var path = process.cwd() + url.substr('leanote:/'.length);
if (path.indexOf('?') >= 0) {
path = path.substr(0, path.indexOf('?'));
}
callback({path: path});
}
}
// var url = request.url.substr(7);
// callback({path: '/Users/life/Desktop/newicon/blog@2x.png'});
@@ -37,6 +51,7 @@ var leanoteProtocol = {
});
// });
}
}
module.exports = leanoteProtocol;

60
node_modules/pdf_main.js generated vendored Normal file
View File

@@ -0,0 +1,60 @@
var app = require('app');
var BrowserWindow = require('browser-window');
var ipc = require('ipc');
var fs = require('fs');
var exportPdf = {
export: function (htmlPath, targetPdfPath, isMarkdown, callbcak) {
var win = new BrowserWindow({
width: 800,
show: false
});
// 写入html, 然后加载这个html
win.loadUrl('file://' + htmlPath);
win.webContents.on('did-finish-load', function() {
console.log('load ok');
setTimeout(function() {
win.printToPDF({
printBackground: true,
landscape: false,
pageSize: 'A4'
}, function(err, data) {
fs.writeFile(targetPdfPath, data, function(err) {
if (err) {
callbcak(false);
}
else {
callbcak(true);
}
// win.close();
});
})
}, isMarkdown ? 1000 : 100);
});
// win.openDevTools();
win.webContents.on('did-fail-load', function() {
callbcak(false);
});
},
init: function () {
var me = this;
ipc.on('export-pdf', function(event, args) {
// event.sender.send();
console.log(args);
me.export(args.htmlPath, args.targetPdfPath, args.isMarkdown, function (ok) {
// console.log('导出pdf');
// console.log(ok);
event.sender.send('export-pdf-ret', {ok: ok, seq: args.seq});
});
});
}
}
module.exports = exportPdf;

View File

@@ -4,6 +4,7 @@ var Config = {
"import_leanote",
"import_evernote",
"export_pdf",
"export_pdf2",
"export_html",
"export_leanote",
"export_evernote",

View File

@@ -1,3 +1,4 @@
// 只有api的插件才能访问
var Api = {
notebook: Notebook,
@@ -16,6 +17,7 @@ var Api = {
noteService: NoteService,
userService: UserService,
dbService: db,
ipc: nodeRequire('ipc'),
// 打开本地目录
// mac和windows下不同

File diff suppressed because one or more lines are too long

488
public/libs/md2html/uml.js Normal file

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,361 @@
/**
* 导出PDF插件2
* @author life life@leanote.com
* 选择目录, 将图片保存到文件夹中, 有个html文件(以笔记名命名)
* 注意, fs.existsSync总返回false, readFileSync可用
*/
define(function() {
var async; // = require('async');
var exportPDF = {
langs: {
'en-us': {
'export': 'Export PDF2',
'Exporting': 'Exporting',
'Exporting: ': 'Exporting: ',
'exportSuccess': 'PDF saved successful!',
'exportFailure': 'PDF saved failure!',
'notExists': 'Please sync your note to ther server firslty.'
},
'zh-cn': {
'export': '导出PDF2',
'Exporting': '正在导出',
'Exporting: ': '正在导出: ',
'exportSuccess': 'PDF导出成功!',
'exportFailure': 'PDF导出失败!'
},
'zh-hk': {
'export': '導出PDF2',
'Exporting': '正在導出',
'Exporting: ': '正在導出: ',
'exportSuccess': 'PDF導出成功!',
'exportFailure': 'PDF導出失敗!'
}
},
_inited: false,
init: function() {
var me = this;
if (me._inited) {
return;
}
async = require('async');
me._inited = true;
me._initExportPdf();
},
// 调用main导出pdf
_exportPdfSeq: 1,
_exportPdfCallback: {},
_initExportPdf: function () {
var me = this;
Api.ipc.on('export-pdf-ret', function(arg) {
var seq = arg.seq;
// console.log('export-pdf-ret');
// console.log(arg);
// console.log(me._exportPdfCallback[seq]);
if (me._exportPdfCallback[seq]) {
me._exportPdfCallback[seq](arg);
}
});
},
exportPdf: function (htmlPath, targetPdfPath, isMarkdown, callback) {
this._exportPdfSeq++;
this._exportPdfCallback[this._exportPdfSeq] = callback;
Api.ipc.send('export-pdf',
{htmlPath: htmlPath, targetPdfPath: targetPdfPath, isMarkdown: isMarkdown, seq: this._exportPdfSeq});
},
getPluginPath: function() {
return Api.evtService.getProjectBasePath() + '/public/plugins/export_pdf2' ;
},
htmlTpl: '',
markdownTpl: '',
getTpl: function(isMarkdown) {
var tpl = isMarkdown ? this.markdownTpl : this.htmlTpl;
if(tpl) {
return tpl;
}
var basePluginPath = this.getPluginPath();
var tplName = isMarkdown ? 'markdown' : 'html';
var tplPath = basePluginPath + '/tpl/' + tplName + '.tpl';
tpl = Api.nodeFs.readFileSync(tplPath, 'utf-8');
isMarkdown ? (this.markdownTpl = tpl) : (this.htmlTpl = tpl);
return tpl;
},
// 生成html或markdown
render: function(note) {
var tpl = this.getTpl(note.IsMarkdown);
var title = note.Title || getMsg('Untitled');
tpl = tpl.replace(/\{title\}/g, title);
tpl = tpl.replace("{content}", function () {
return note.Content; // 为什么要这样? 因为 $$ 替换后变成了一个!!
});
return tpl;
},
replaceAll: function(src, pattern, to) {
if(!src) {
return src;
}
while(true) {
var oldSrc = src;
src = src.replace(pattern, to);
if(oldSrc === src) {
return src;
}
}
},
fixFilename: function(filename) {
var reg = new RegExp("/|#|\\$|!|\\^|\\*|'| |\"|%|&|\\(|\\)|\\+|\\,|/|:|;|<|>|=|\\?|@|\\||\\\\", 'g');
filename = filename.replace(reg, "-");
// 防止出现两个连续的-
while(filename.indexOf('--') != -1) {
filename = this.replaceAll(filename, '--', '-');
}
if (filename.length > 1) {
// 最后一个-
filename = filename.replace(/\-$/, '');
}
return filename;
},
// 得到可用的文件名, 避免冲突
getPdfFilePath: function(pathInfo, n, cb) {
var me = this;
if(n > 1) {
pathInfo.nameNotExt = pathInfo.nameNotExtRaw + '-' + n;
}
var absPath = pathInfo.getFullPath();
// Api.nodeFs.existsSync(absPath) 总是返回false, 不知道什么原因
// 在控制台上是可以的
Api.nodeFs.exists(absPath, function(exists) {
if(!exists) {
cb(absPath);
}
else {
me.getPdfFilePath(pathInfo, n+1, cb);
}
});
},
getTargetPath: function(callback) {
// showSaveDialog 不支持property选择文件夹
Api.gui.dialog.showOpenDialog(Api.gui.getCurrentWindow(),
{
defaultPath: Api.gui.app.getPath('userDesktop') + '/',
properties: ['openDirectory']
},
function(targetPath) {
callback(targetPath);
}
);
},
loadingIsClosed: false,
exportPDFForNotebook: function (notebookId) {
var me = this;
if (!notebookId) {
return;
}
me.getTargetPath(function(targetPath) {
if (!targetPath) {
return;
}
me.loadingIsClosed = false;
Api.loading.show(Api.getMsg('plugin.export_pdf2.Exporting'),
{
hasProgress: true,
isLarge: true,
onClose: function () {
me.loadingIsClosed = true;
setTimeout(function() {
me.hideLoading();
});
}});
Api.loading.setProgress(1);
Api.noteService.getNotes(notebookId, function(notes) {
if (!notes) {
me.hideLoading();
return;
}
var total = notes.length;
var i = 0;
async.eachSeries(notes, function(note, cb) {
if (me.loadingIsClosed) {
cb();
me.hideLoading();
return;
}
i++;
Api.loading.setProgress(100 * i / total);
me._exportPDF(note, targetPath, function() {
cb();
}, i, total);
}, function() {
me.hideLoading();
Notify.show({title: 'Info', body: getMsg('plugin.export_pdf2.exportSuccess')});
});
});
});
},
hideLoading: function () {
setTimeout(function () {
Api.loading.hide();
}, 1000);
},
exportPDF: function (noteIds) {
var me = this;
if (!noteIds || noteIds.length == 0) {
return;
}
me.getTargetPath(function(targetPath) {
if (!targetPath) {
return;
}
me.loadingIsClosed = false;
Api.loading.show(Api.getMsg('plugin.export_pdf2.Exporting'),
{
hasProgress: true,
isLarge: true,
onClose: function () {
me.loadingIsClosed = true;
setTimeout(function() {
me.hideLoading();
});
}});
Api.loading.setProgress(1);
var i = 0;
var total = noteIds.length;
async.eachSeries(noteIds, function(noteId, cb) {
if (me.loadingIsClosed) {
cb();
return;
}
i++;
Api.loading.setProgress(100 * i / total);
Api.noteService.getNote(noteId, function(note) {
me._exportPDF(note, targetPath, function(ok) {
cb();
}, i, total);
});
}, function () {
me.hideLoading();
Notify.show({title: 'Info', body: getMsg('plugin.export_pdf2.exportSuccess')});
});
});
},
_exportPDF: function(note, path, callback, i, total) {
var me = this;
setTimeout(function () {
me._exportPDF2(note, path, callback, i, total);
}, 1000);
},
_exportPDF2: function(note, path, callback, i, total) {
var me = this;
if(!note) {
return;
}
if (me.loadingIsClosed) {
callback();
return;
}
setTimeout(function () {
Api.loading.setMsg(Api.getMsg('plugin.export_pdf2.Exporting: ') + (note.Title || getMsg('Untitled')));
Api.loading.setProgressRate(i + '/' + total);
}, 100);
var name = note.Title ? note.Title + '.pdf' : getMsg('Untitled') + '.pdf';
name = me.fixFilename(name);
var targetPath = path + '/' + name;
// 将路径和名字区分开
var pathInfo = Api.commonService.splitFile(targetPath);
pathInfo.nameNotExt = me.fixFilename(pathInfo.nameNotExt); // 重新修正一次
var nameNotExt = pathInfo.nameNotExt;
pathInfo.nameNotExtRaw = pathInfo.nameNotExt;
// 得到可用文件的绝对路径
me.getPdfFilePath(pathInfo, 1, function(absPdfFilePath) {
// 得到存放assets的目录
var html = me.render(note);
var tempPath = Api.gui.app.getPath('temp');
var last = tempPath[tempPath.length-1];
if ( last == '/' || last == '\\') {
tempPath = tempPath.substr(0, tempPath.length - 1);
}
var targetHtmlPath = tempPath + '/' + (new Date().getTime()) + '.html';
// 把html文件写到tmp目录
Api.commonService.writeFile(targetHtmlPath, html);
me.exportPdf(targetHtmlPath, absPdfFilePath, note.IsMarkdown, function (args) {
// console.log('export pdf ret');
callback(args.ok);
});
});
},
// 打开前要执行的
onOpen: function() {
var me = this;
var gui = Api.gui;
var menu = {
label: Api.getMsg('plugin.export_pdf2.export'),
enabled: function(noteIds) {
return true;
},
click: (function() {
return function(noteIds) {
me.init();
me.exportPDF(noteIds);
}
})()
};
Api.addExportMenu(menu);
Api.addExportMenuForNotebook({
label: Api.getMsg('plugin.export_pdf2.export'),
enabled: function(notebookId) {
return true;
},
click: (function() {
return function(notebookId) {
me.init();
me.exportPDFForNotebook(notebookId);
}
})()
});
},
// 打开后
onOpenAfter: function() {
},
// 关闭时需要运行的
onClose: function() {
}
};
return exportPDF;
});

View File

@@ -0,0 +1,155 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{title}</title>
<style>
*{font-size:16px}
*{font-family:"lucida grande","lucida sans unicode",lucida,helvetica,"Hiragino Sans GB","Microsoft YaHei","WenQuanYi Micro Hei",sans-serif;}
body {
margin: 0;
}
h1{font-size:30px}h2{font-size:24px}h3{font-size:18px}h4{font-size:14px}
.note-container{
width:850px;
margin:auto;
padding: 10px 20px;
}
#title {
margin: 0;
}
table {
margin-bottom: 16px;
border-collapse: collapse;
}
table th, table td {
padding: 6px 13px;
border: 1px solid #ddd;
}
table th {
font-weight: bold;
}
table tr {
background-color: none;
border-top: 1px solid #ccc;
}
table tr:nth-child(2n) {
background-color: rgb(247, 247, 249);
}
.mce-item-table, .mce-item-table td, .mce-item-table th, .mce-item-table caption {
border: 1px solid #ddd;
border-collapse: collapse;
padding: 6px 13px;
}
blockquote {
border-left-width:10px;
background-color:rgba(128,128,128,0.05);
border-top-right-radius:5px;
border-bottom-right-radius:5px;
padding:15px 20px;
border-left:5px solid rgba(128,128,128,0.075);
}
blockquote p {
margin-bottom:1.1em;
font-size:1em;
line-height:1.45
}
blockquote ul:last-child,blockquote ol:last-child {
margin-bottom:0
}
pre {
padding: 18px;
background-color: #f7f7f9;
border: 1px solid #e1e1e8;
border-radius: 3px;
display: block;
}
code {
padding: 2px 4px;
font-size: 90%;
color: #c7254e;
white-space: nowrap;
background-color: #f9f2f4;
border-radius: 4px;
}
.footnote {
vertical-align: top;
position: relative;
top: -0.5em;
font-size: .8em;
}
hr {
margin:2em 0
}
img {
max-width:100%
}
pre {
word-break:break-word
}
p,pre,pre.prettyprint,blockquote {
margin:0 0 1.1em
}
hr {
margin:2em 0
}
.sequence-diagram,.flow-chart {
text-align:center;
margin-bottom:1.1em
}
.sequence-diagram text,.flow-chart text {
font-size:15px !important;
font-family:"Source Sans Pro",sans-serif !important
}
.sequence-diagram [fill="#ffffff"],.flow-chart [fill="#ffffff"] {
fill:#f6f6f6
}
.sequence-diagram [stroke="#000000"],.flow-chart [stroke="#000000"] {
stroke:#3f3f3f
}
.sequence-diagram text[stroke="#000000"],.flow-chart text[stroke="#000000"] {
stroke:none
}
.sequence-diagram [fill="#000"],.flow-chart [fill="#000"],.sequence-diagram [fill="#000000"],.flow-chart [fill="#000000"],.sequence-diagram [fill="black"],.flow-chart [fill="black"] {
fill:#3f3f3f
}
ul,ol {
margin-bottom:1.1em
}
ul ul,ol ul,ul ol,ol ol {
margin-bottom:1.1em
}
kbd {
padding:.1em .6em;
border:1px solid rgba(63,63,63,0.25);
-webkit-box-shadow:0 1px 0 rgba(63,63,63,0.25);
box-shadow:0 1px 0 rgba(63,63,63,0.25);
font-size:.7em;
font-family:sans-serif;
background-color:#fff;
color:#333;
border-radius:3px;
display:inline-block;
margin:0 .1em;
white-space:nowrap
}
.toc ul {
list-style-type:none;
margin-bottom:15px
}
</style>
</head>
<body>
<div class="note-container">
<h1 class="title" id="leanote-title">{title}</h1>
<div class="content-html" id="leanote-content">{content}</div>
</div>
</body>
</html>

View File

@@ -0,0 +1,187 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{title}</title>
<style>
*{font-size:16px}
*{font-family:"lucida grande","lucida sans unicode",lucida,helvetica,"Hiragino Sans GB","Microsoft YaHei","WenQuanYi Micro Hei",sans-serif;}
body {
margin: 0;
}
h1{font-size:30px}h2{font-size:24px}h3{font-size:18px}h4{font-size:14px}
.note-container{
width:850px;
margin:auto;
padding: 10px 20px;
}
#title {
margin: 0;
}
table {
margin-bottom: 16px;
border-collapse: collapse;
}
table th, table td {
padding: 6px 13px;
border: 1px solid #ddd;
}
table th {
font-weight: bold;
}
table tr {
background-color: none;
border-top: 1px solid #ccc;
}
table tr:nth-child(2n) {
background-color: rgb(247, 247, 249);
}
.mce-item-table, .mce-item-table td, .mce-item-table th, .mce-item-table caption {
border: 1px solid #ddd;
border-collapse: collapse;
padding: 6px 13px;
}
blockquote {
border-left-width:10px;
background-color:rgba(128,128,128,0.05);
border-top-right-radius:5px;
border-bottom-right-radius:5px;
padding:15px 20px;
border-left:5px solid rgba(128,128,128,0.075);
}
blockquote p {
margin-bottom:1.1em;
font-size:1em;
line-height:1.45
}
blockquote ul:last-child,blockquote ol:last-child {
margin-bottom:0
}
pre, code {
font-family: 'Monaco', 'Menlo', 'Ubuntu Mono', 'Consolas', 'source-code-pro', Hiragino Sans GB, "Microsoft YaHei", "微软雅黑";
}
pre {
padding: 18px;
background-color: #f7f7f9;
border: 1px solid #e1e1e8;
border-radius: 3px;
display: block;
}
code {
padding: 2px 4px;
font-size: 90%;
color: #c7254e;
white-space: nowrap;
background-color: #f9f2f4;
border-radius: 4px;
}
.footnote {
vertical-align: top;
position: relative;
top: -0.5em;
font-size: .8em;
}
hr {
margin:2em 0
}
img {
max-width:100%;
/*display: block;*/
/*margin: auto;*/
}
pre {
word-break:break-word
}
p,pre,pre.prettyprint,blockquote {
margin:0 0 1.1em
}
hr {
margin:2em 0
}
.sequence-diagram,.flow-chart {
text-align:center;
margin-bottom:1.1em
}
.sequence-diagram text,.flow-chart text {
font-size:15px !important;
font-family:"Source Sans Pro",sans-serif !important
}
.sequence-diagram [fill="#ffffff"],.flow-chart [fill="#ffffff"] {
fill:#f6f6f6
}
.sequence-diagram [stroke="#000000"],.flow-chart [stroke="#000000"] {
stroke:#3f3f3f
}
.sequence-diagram text[stroke="#000000"],.flow-chart text[stroke="#000000"] {
stroke:none
}
.sequence-diagram [fill="#000"],.flow-chart [fill="#000"],.sequence-diagram [fill="#000000"],.flow-chart [fill="#000000"],.sequence-diagram [fill="black"],.flow-chart [fill="black"] {
fill:#3f3f3f
}
ul,ol {
margin-bottom:1.1em
}
ul ul,ol ul,ul ol,ol ol {
margin-bottom:1.1em
}
kbd {
padding:.1em .6em;
border:1px solid rgba(63,63,63,0.25);
-webkit-box-shadow:0 1px 0 rgba(63,63,63,0.25);
box-shadow:0 1px 0 rgba(63,63,63,0.25);
font-size:.7em;
font-family:sans-serif;
background-color:#fff;
color:#333;
border-radius:3px;
display:inline-block;
margin:0 .1em;
white-space:nowrap
}
.toc ul {
list-style-type:none;
margin-bottom:15px
}
.m-todo-item {
list-style: none;
}
pre code {
padding:0;font-size:inherit;color:inherit;
white-space:pre-wrap;
background-color:transparent;
border-radius:0;
}
</style>
</head>
<body>
<div class="note-container">
<h1 class="title" id="leanote-title">{title}</h1>
<div class="content-container html" id="content-container">
<!-- markdown -->
<textarea id="leanote-content-markdown" style="display: none">{content}</textarea>
<!-- html -->
<div class="content-html" id="leanote-content-html"></div>
</div>
</div>
<script src="leanote://public/libs/md2html/md2html.js"></script>
<!-- <script src="http://leanote.com/public/libs/md2html/md2html_for_export.js"></script> -->
<script>
function init() {
md2Html(document.getElementById('leanote-content-markdown').value, document.getElementById('leanote-content-html'), function(html) {
// console.log('解析完成');
});
}
init();
</script>
</body>
</html>