-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
112 lines (99 loc) · 3.71 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
document.getElementById('uploadFile').addEventListener('change', handleFileUpload);
document.getElementById('convertButton').addEventListener('click', convertFile);
let uploadedFile = null;
function handleFileUpload(event) {
uploadedFile = event.target.files[0];
alert(event.target.files[0]);
}
function processFile(file) {
if (file.name.endsWith('.docx')) {
loadDocx(file);
} else if (file.name.endsWith('.pdf')) {
loadPdf(file)
} else {
alert('Unsupported file type');
}
}
function convertFile() {
if (uploadedFile) {
processFile(uploadedFile);
} else {
alert('No file selected');
}
}
function loadDocx(file) {
var reader = new FileReader(file);
reader.onload = function(event) {
var arrayBuffer = reader.result;
mammoth.convertToHtml({arrayBuffer: arrayBuffer})
.then(function(result) {
document.getElementById('editor').innerHTML = result.value;
})
.catch(function(err) {
console.log(err);
});
};
reader.readAsArrayBuffer(file);
}
function loadPdf(file) {
var fileReader = new FileReader();
fileReader.onload = function() {
var typedarray = new Uint8Array(this.result);
var loadingTask = pdfjsLib.getDocument({data: typedarray});
loadingTask.promise.then(function(pdf) {
console.log('PDF loaded');
var totalPages = pdf.numPages;
var content = ''"";
function renderPage(pageNum) {
pdf.getPage(pageNum).then(function(page) {
var scale = 1.5;
var viewport = page.getViewport({scale: scale});
var canvas = document.createElement('canvas');
var ctx = canvas.getContext('2d');
var renderContext = {
canvasContext: ctx,
viewport: viewport
};
canvas.height = viewport.height;
canvas.width = viewport.width;
page.render(renderContext).promise.then(function () {
content += '<img src="' + canvas.toDataURL() + '"/>';
if (pageNum < totalPages) {
renderPage(pageNum + 1);
} else {
document.getElementById('editor').innerHTML = content;
}
});
});
}
renderPage(1);
}, function(reason) {
console.error(reason);
});
};
fileReader.readAsArrayBuffer(file);
}
function saveAsDocx() {
var editorContent = document.getElementById('editor').innerHTML;
var html = '<!DOCTYPE html><html><head><meta charset="utf-8"></head><body>' + editorContent + '</body></html>';
var converted = htmlDocx.asBlob(html);
saveAs(converted, 'document.docx');
}
function saveAsHtml() {
var htmlContent = document.getElementById('editor').innerHTML;
var blob = new Blob([htmlContent], {type: "text/html;charset=utf-8"});
saveAs(blob, "document.html");
}
function saveAsMarkdown() {
var turndownService = new TurndownService();
var markdown = turndownService.turndown(document.getElementById('editor').innerHTML);
var blob = new Blob([markdown], {type: "text/plain;charset=utf-8"});
saveAs(blob, "document.md");
}
function saveAsPdf() {
var editorContent = document.getElementById('editor').innerHTML;
var pdfWindow = window.open('', '_blank');
pdfWindow.document.write('<html><head><title>PDF</title></head><body>' + editorContent + '</body></html>');
pdfWindow.document.close();
pdfWindow.print();
}