修复导出Word、PDF的一些bug, 仍有bug

master
joefalmko 3 weeks ago
parent 67f8a61501
commit 7a757634b4

@ -19,8 +19,8 @@
</template>
<script>
import {
DecoupledEditor,
// ClassicEditor,
// DecoupledEditor,
ClassicEditor,
AccessibilityHelp,
Alignment,
Autoformat,
@ -114,6 +114,20 @@ import { saveAs } from 'file-saver';
// import Collection from "@ckeditor/ckeditor5-utils/src/collection";
// import {addListToDropdown,createDropdown} from "@ckeditor/ckeditor5-ui/src/dropdown/utils";
// import SplitButtonView from "@ckeditor/ckeditor5-ui/src/dropdown/button/splitbuttonview";
//
function getStyle(){
let str = '<head><meta charset="utf-8"></meta>';
const styles = document.querySelectorAll('style');
for(let i = 0; i < styles.length; i++){
str+=styles[i].outerHTML;
}
str += "<style>.results{width:100%!important;} .result .title{width:100%;}</style>";
str += "<style>table{border-collapse: collapse;table-layout: fixed}</style>"
// str += "<style>table thead tr{ background-color: #f3f4f9;}</style>"
str += "<style>table td,th{ font-size: 14px;padding: 1px 1px;border-width: 1px;border-style: solid;border-color: #d0d0d0;word-break: keep-all;white-space: nowrap;}</style>"
return str;
}
class Export2Word extends Plugin {
init() {
const editor = this.editor;
@ -134,9 +148,10 @@ class Export2Word extends Plugin {
// Execute a callback function when the button is clicked
button.on('execute', () => {
// console.log(editor.getData());
const pageContent = document.querySelector("#app > div > div > div > div.editor-container__editor-wrapper > div > div > div").innerHTML;
const page = '<!DOCTYPE html><html><head><meta charset="UTF-8"></head><body>' + pageContent + '</body></html>'
const pageContent = document.querySelector("#app > div > div > div > div.editor-container__editor-wrapper > div > div > div.ck.ck-reset.ck-editor.ck-rounded-corners > div.ck.ck-editor__main")
const style=getStyle();
const page = '<!DOCTYPE html><html><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><style>@media print{@page{size:A4 portrait;margin:0cm 3cm}@page:left{margin-left:2.5cm;margin-right:2.7cm;}@page:right{margin-left:2.7cm;margin-right:2.5cm;}}</style>'+style+'</head><body>' + pageContent.outerHTML + '</body></html>'
// console.log(page);
asBlob(page).then(data => {
saveAs(data, 'file.docx') // save as docx file
@ -186,31 +201,16 @@ class Export2PDF extends Plugin{
// Execute a callback function when the button is clicked
button.on('execute', () => {
// console.log(editor.getData());
const pageContent = document.querySelector("#app > div > div > div > div.editor-container__editor-wrapper > div > div > div");
// pageContent.className+=' print-content';
const page = '<!DOCTYPE html><html><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><style>@media print{@page{size:A4 portrait;margin:0cm 3cm}@page:left{margin-left:2.5cm;margin-right:2.7cm;}@page:right{margin-left:2.7cm;margin-right:2.5cm;}}</style></head><body>' + pageContent.innerHTML + '</body></html>'
// console.log(page);
const pageContent = document.querySelector("#app > div > div > div > div.editor-container__editor-wrapper > div > div > div.ck.ck-reset.ck-editor.ck-rounded-corners > div.ck.ck-editor__main");
const style=getStyle();
const page = '<!DOCTYPE html><html><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><style>@media print{@page{size:A4 portrait;margin:0cm 3cm}@page:left{margin-left:2.5cm;margin-right:2.7cm;}@page:right{margin-left:2.7cm;margin-right:2.5cm;}}</style>'+style+'</head><body>' + pageContent.outerHTML + '</body></html>'
const newWindow = window.open('','PrintDocument', 'height=600,width=700,top=50,left=50');
newWindow.document.write(page);
newWindow.document.close();
// newWindow.document.addEventListener('afterprint', function () {
// //
// newWindow.close();
// }, false);
newWindow.onload = function(){
newWindow.print();
// // newWindow.close();
}
newWindow.print();
newWindow.onafterprint=function(){
newWindow.close();
}
// asBlob(page).then(data => {
// saveAs(data, 'file.docx') // save as docx file
// }); // asBlob() return Promise<Blob|Buffer>
});
return button;
@ -293,8 +293,8 @@ export default {
return {
isLayoutReady: false,
config: null, // CKEditor needs the DOM tree before calculating the configuration.
editor: DecoupledEditor
// editor:ClassicEditor
// editor: DecoupledEditor
editor:ClassicEditor
};
},
mounted() {

Loading…
Cancel
Save