简体   繁体   English

html2pdf外部图像不显示

[英]html2pdf external image not showing

I have an MVC app utilizing html2pdf to convert html into a PDF document with javascript.我有一个 MVC 应用程序,它利用html2pdf将 html 转换为带有 javascript 的 PDF 文档。 The html that is being converted is a customer invoice with an image of their logo.正在转换的 html 是带有其徽标图像的客户发票。 We utilize Azure Blob storage to store our images.我们利用 Azure Blob 存储来存储我们的图像。 Since Azure blob storage is hosted all our images show by placing the URL of where it is located in Azure blob storage.由于托管了 Azure blob 存储,因此我们通过将其所在位置的 URL 放置在 Azure blob 存储中来显示所有图像。

I have been able to convert the html to a pdf but the external url images are not displaying in the pdf, just a blank space shows up.我已经能够将 html 转换为 pdf,但是外部 url 图像没有显示在 pdf 中,只显示了一个空格。

I am receiving this error:我收到此错误:

Access to image at 'https://blob.core.windows.net/images/a5a1032bf7b3b0ae4f70/profile/2064/Transparent_Logo_Blue.png' 
from origin 'http://localhost:65426' 
has been blocked by CORS policy: 
No 'Access-Control-Allow-Origin' 
header is present on the requested resource.

I have enabled cors, enabled cross origin on the img tag.我在 img 标签上启用了 cors,启用了跨域。 I have tried changin type: 'png' to jpg and jpeg .我尝试将 type: 'png'更改为jpgjpeg All with the same errors.都有相同的错误。

My code:我的代码:

 function downloadPDF() {

        //var element = document.getElementById('invoice');
        //html2pdf(element);

        // Get the element to print
        var element = document.getElementById('invoice');

        // Define optional configuration
        var options = {
            filename: 'inv_' + $('#InvoiceMaster_InvoiceNumber').val() + '.pdf',
            image: { type: 'png', quality: 0.98 },
            html2canvas: { scale: 2, allowTaint: false, useCORS: true },
            jsPDF: { unit: 'in', format: 'letter', orientation: 'portrait' }
        };

        // Create instance of html2pdf class
        var exporter = new html2pdf(element, options);

        // You can also use static methods for one time use...
        options.source = element;
        options.download = true;
        /*html2pdf.getPdf(options);*/

    }

May be it´s something related with azure blob access permissions.可能与 azure blob 访问权限有关。

I don´t know if it´s your case, but may be that you need to add some information in your header requests as it has been explained on that document.我不知道这是否是您的情况,但可能是您需要在标头请求中添加一些信息,正如该文档中已解释的那样。

https://docs.microsoft.com/en-us/rest/api/storageservices/preflight-blob-request https://docs.microsoft.com/en-us/rest/api/storageservices/preflight-blob-request

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM