簡體   English   中英

使用外部樣式表將 HTML 導出到 DocX

[英]Export HTML to DocX with external Stylesheet

我使用QuillJs允許用戶在我的項目中編輯文本,在我的網絡應用程序中實現它非常容易(我使用 Laravel 作為后端,使用 HTML/CSS/JS 作為前端),以及我想添加將自定義文本導出到 Docx 和 PDF 的功能,但問題是, QuillJs使用外部樣式表來構建編輯后的文本,因此導出時文本的某些部分不顯示任何樣式我試過用PHPWord和 Vanilla JavaScript 導出它,它們似乎都只允許內聯樣式。 我的JS函數:

const Foo = () => {
    var header = "<html xmlns:o='urn:schemas-microsoft-com:office:office' " +
            "xmlns:w='urn:schemas-microsoft-com:office:word' " +
            "xmlns='http://www.w3.org/TR/REC-html40'>" +
            "<head><meta charset='utf-8'>" +
            "<link type=\"text/css\" href=\"https://cdn.quilljs.com/1.3.6/quill.snow.css\" rel=\"stylesheet\">" +
            "</head><body>";
    var footer = "</body></html>";
    var body = document.querySelector('.ql-editor').innerHTML;
    var sourceHTML = header + body + footer;
    var source = 'data:application/vnd.ms-word;charset=utf-8,' + encodeURIComponent(sourceHTML);
    // console.log(source);
    var fileDownload = document.createElement("a");
    document.body.appendChild(fileDownload);
    fileDownload.href = source;
    fileDownload.download = 'document.doc';
    fileDownload.click();
    document.body.removeChild(fileDownload);
  }

你可以看到我已經嘗試添加<link type=\\"text/css\\" href=\\"https://cdn.quilljs.com/1.3.6/quill.snow.css\\" rel=\\"stylesheet\\">所以,我的問題是有一種方法可以用樣式導出它,還是有另一個文本編輯器但具有內聯樣式(開源請參閱)?

編輯 1:我嘗試添加var style = (`<style> css ...</style>`) ;

問題是,我已經以一種簡單的方式解決了我的問題,即更改了編輯器框架。 我試過Froala EditorQuillJs ,當我導出到 Docx 時,它們都沒有樣式,所以我搜索了另一個,我找到了Summernote ,一個帶有INLINE STYLE的開源 WYSIWYG 編輯器,現在我可以導出帶有樣式的 Docx 文件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM