【问题标题】:How to add an image into word file in javascript如何在javascript中将图像添加到word文件中
【发布时间】:2021-07-27 12:20:01
【问题描述】:

这是从 javascript 函数创建 word 文件的代码的一部分

我想在文件中添加一张图片(图片必须是服务器硬盘上的物理图片,而不是互联网上的 URL)

如何做到这一点

                   var templateHeader = '<html xmlns:v="urn:schemas-microsoft-com:vml"\
                                xmlns:o="urn:schemas-microsoft-com:office:office"\
                                xmlns:w="urn:schemas-microsoft-com:office:word"\
                                xmlns:m="http://schemas.microsoft.com/office/2004/12/omml"\
                                xmlns:css="http://macVmlSchemaUri" xmlns="http://www.w3.org/TR/REC-html40">\
                                <head>\
                                <meta name=Title content="">\
                                <meta name=Keywords content="">\
                                <meta http-equiv=Content-Type content="text/html; charset=unicode">\
                                <meta name=ProgId content=Word.Document>\
                                <meta name=Generator content="Microsoft Word 14">\
                                <meta name=Originator content="Microsoft Word 14">\
                                <link rel=File-List href="Customer%20(5)_files/filelist.xml">\
                                <!--[if gte mso 9]><xml>\
                                 <w:WordDocument>\
                                  <w:View>Print</w:View>\
                                 </w:WordDocument>\
                                </xml><![endif]-->\
                                <style>';
        
            templateHeader += '<!--@page WORDSECTION1 {mso-page-orientation:potrait;} -->';

        templateHeader += '</style></head><body bgcolor=white lang=EN-US style=\'tab-interval:36.0pt\'><div class=WordSection1>';
        var templateFooter = '<p class=MsoNormal style=\'mso-margin-top-alt:auto;mso-margin-bottom-alt:auto\'><span style=\'mso-fareast-font-family:"Times New Roman";mso-bidi-font-family:"Times New Roman"\'><o:p>&nbsp;</o:p></span></p></div></body></html>';

        var charset = document.characterSet || document.charset;

        saveAs(new Blob(["\ufeff", templateHeader + "This is my file" + templateFooter], {
            type: 'application/msword;charset='+ charset,
            encoding: charset
        }), "filename" + '.doc');

【问题讨论】:

    标签: javascript jquery ms-word ms-office xml-namespaces


    【解决方案1】:

    由于其余部分也是 HTML,因此图像也可以添加为 HTML:

    var image = '<img src="https://www.gravatar.com/avatar/fda292124649edada394b86c22a9103d?s=64&amp;d=identicon&amp;r=PG" width="32" height="32">';
    function getDataUri(url, callback) {
        var image = new Image();
    
        image.onload = function () {
            var canvas = document.createElement('canvas');
            canvas.width = this.naturalWidth; // or 'width' if you want a special/scaled size
            canvas.height = this.naturalHeight; // or 'height' if you want a special/scaled size
    
            canvas.getContext('2d').drawImage(this, 0, 0);
    
            // Get raw image data
            callback(canvas.toDataURL('image/png').replace(/^data:image\/(png|jpg);base64,/, ''));
    
            // ... or get as Data URI
            callback(canvas.toDataURL('image/png'));
        };
    
        image.src = url;
    }
    
    // Usage
    getDataUri(image, function(dataUri) {
       saveAs(new Blob(["\ufeff", templateHeader + "This is my file" + dataUri + templateFooter], {
            type: 'application/msword;charset='+ charset,
            encoding: charset
        }), "filename" + '.doc');
    });
    

    【讨论】:

    • 这只会在通过 Internet 连接到服务器时加载图像,或者如果 Web 应用程序位于 Intranet 上,则在本地加载图像。即使未连接到服务器,是否也可以加载?
    猜你喜欢
    • 2020-02-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多