使用HtmlRenderer.PdfSharp将图像加载到PDF中时出现问题 [英] Problem loading images into PDF using HtmlRenderer.PdfSharp

查看:487
本文介绍了使用HtmlRenderer.PdfSharp将图像加载到PDF中时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这有点远,但是也许有人可能有一些想法.

This is a bit of a longshot, but maybe someone might have some ideas.

供参考-参见图像无法显示为PDF ,这是一个类似的问题

For reference - see Images don't display in PDF, a similar question.

我正在使用Htmlrenderer.PdfSharp库从HTML内容创建PDF文件.一切工作正常,唯一的例外是图像.他们只是显示一个大红色框.

I'm using the Htmlrenderer.PdfSharp library to create PDF files from HTML content. Everything works perfectly, with the sole exception of the images. They simply display a big red box.

以调试模式在本地运行时,PDF可以正常工作,但在服务器上部署时,则不能正常工作.我还有一些其他信息可能会有所帮助-在执行创建PDF的命令时:

The PDF works fine when running locally in debug mode, but not when deployed on the server. I've a small bit of extra information that might help - when executing the command to create the PDF:

        using (MemoryStream ms = new MemoryStream())
        {
            var pdf = TheArtOfDev.HtmlRenderer.PdfSharp.PdfGenerator.GeneratePdf(htmlContent, PdfSharp.PageSize.A4);
            pdf.Save(ms);
            res = ms.ToArray();
        }

在服务器上,出现以下一些错误:

On the server, I get a few of the following errors:

Exception thrown: 'System.Security.Authentication.AuthenticationException' in System.dll

在本地运行,我没有得到这些错误.我觉得它与.NET引擎有关,无法正确解析图像URL.就是说,图片网址是完全合格的.但是,如果有帮助,它就位于HTTPS后面.

Running locally I don't get those errors. I have a feeling it has something to do with the .NET engine not being able to properly resolve the image URL. That being said, the image URL is fully-qualified. It is, however, behind HTTPS, if that helps.

我无法进一步调试.如果有人有任何想法,我很想听听他们的想法-即使在更多的地方,我也可以尝试寻找线索.

I'm unable to go any further with my debugging. If anyone has any ideas, I'd love to hear them - even if it is just more places I can try hunting for clues.

推荐答案

经过大量的实验,我找到了一个解决方案.这是允许该解决方案运行的内部版本的参考,因此请确保您具有正确的版本:

After quite a bit of experimenting, I've found a solution. Here's a reference to the build that allows this solution to work, so be sure you have the right version:

https://github.com/ArthurHub/HTML-Renderer/pull/41

解决方案是将有问题的图像加载到字节数组中,然后通过使用图像的base64字符串表示形式将图像直接加载到HTML中:

The solution is to load the image in question into a byte array, and then load the image directly into the HTML by using a base64 string representation of the image:

byte[] array = File.ReadAllBytes(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"[YourImageFilePathHere]"));

这将从项目的部署路径中的文件夹中获取文件-在我的情况下,我有一个名为images的文件夹,其中有问题的图像位于其中.

This will take the file from a folder within your project's deployed path - in my case, I have a folder called images, within which my image in question is located.

接下来,在CSS内(或者,如果需要,可以内联):

Next, within the CSS (or, optionally, inline if you'd like):

htmlContent += " img.logo { width:110px;height:110px;content: url('data:image/jpeg;base64," + Convert.ToBase64String(array) +"')} ";

最后,在HTML中:

string imageUrl = "<img class=\"logo\"></img>";

就是这样!对我来说很完美.

And that's it! Worked perfectly for me.

这篇关于使用HtmlRenderer.PdfSharp将图像加载到PDF中时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆