我正在尝试将包含图像和 css 文件(例如“bootstrap”)的 Azure 网站中的 HTML 转换为 PDF。
我的代码(与本案例的教程相同 - MVC c# - http://www.html2pdfrocket.com/Examples/CSharp)
string apiKey = "MY KEY";
string value = html; // SOME HTML IN STRING FORMAT
using (var client = new WebClient())
{
// Build the conversion options
NameValueCollection options = new NameValueCollection();
options.Add("apikey", apiKey);
options.Add("value", value);
// Call the API convert to an image
byte[] result = client.UploadValues("http://api.html2pdfrocket.com/pdf", options);
// Save the image to disk
System.IO.File.WriteAllBytes(Server.MapPath("~/PDF/somepdf.pdf"), result);
}
代码生成 pdf 文件,但没有 css 样式和图像。
如何解决这个问题?
html 非常简单,只有一张表格、一张图片和一个 css 文件“bootstrap”
我想在html中显示的图像放置如下:
<img id="image" src="~/Images/someimage.jpg" >
CSS 文件是:
<link href="~/Scripts/bootstrap/dist/css/bootstrap.css" rel="stylesheet" />
我也有一些像这样的 CSS 类:
#header {
padding: 0;
margin: 0;
float: left;
position: relative;
min-height: 1px;
font-size: 12px;
}
我已经尝试放置图像和“ Bootstrap ”的完整路径,但它仍然不起作用。
最佳答案
您只需将所有链接更改为完整地址,例如
<link href="https://www.example.com/Scripts/bootstrap/dist/css/bootstrap.css" rel="stylesheet" />
或者创建一个 html 页面并将该 html 页面的 url 发送到 API。
关于HTML 到 PDF Rocket - PDF 生成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30086727/