内存中的 pdfHTML CSS
pdfHTML with in-memory CSS
我正在试用 iText7 并尝试拼凑如何做事。似乎我可以放入一个基本 URI 来获取外部资源,我假设如果它找到一个 .css 它会应用它吗?我有一种特殊情况,我更容易将 CSS 作为字符串保存在内存中。我可以使用 HtmlConverter.convertToPdf() 并将 HTML 作为字符串传递而不是 CSS.
似乎很奇怪
作为次要问题,如果它在该基本 URI 处找到多个 CSS 文件会怎样?
最后(抱歉转储),如果 HTML 包含图像的 FQDN URL,我 assuming/hoping 它会直接拉取图像吗?换句话说,我希望我不必 store/write 那些图像到指定的基本 URI?
谢谢。
更新: 我整理了一个快速演示。我发现它 将 下载具有完整 URL 的图像,这很棒。
但是,它似乎没有加载我指定文件夹中的 CSS 文件。代码:
StringBuilder sb = new StringBuilder(File.ReadAllText("demoHtml.html"));
// this folder, which is relative to the test .exe, contains a file called pdf.css
ConverterProperties props = new ConverterProperties().SetBaseUri("Content/Pdf");
FileStream fs = new FileStream("itext.pdf", FileMode.Create);
HtmlConverter.ConvertToPdf(sb.ToString(), fs, props);
和 CSS:
img {
max-width: 100%;
}
table {
vertical-align: top;
}
td ol {
-webkit-padding-start: 15px;
padding-left: 15px;
}
thead tr {
background: #aaa;
}
tr:nth-child(even) {
background: #eee;
}
问题 1 的解答:
I'm trying out iText7 and trying to piece together how to do things. It seems that I can put in a base URI to grab external resources which I'm assuming if it finds a .css it will apply that? I have a particular situation where it's easier for me to hold the CSS in memory as a string. It seems odd that I can use HtmlConverter.convertToPdf() and pass in HTML as a string but not CSS.
我花了很多时间寻找这个问题的解决方案。一切似乎都是正确的,我什至问了一个关于使用 CSS 文件的支持问题。与 itext5 (itextsharp) 相反,itext7 无法管理其中包含 space 的 url。
因此在本地测试路径如下:c:/Path to project/project/name/wwwroot/ 将不起作用(注意 spaces)
起初我没有注意到这一点,因为我以编程方式生成了指向我的 css 文件夹的路径:
var basepath = env.ContentRootPath + "\wwwroot\pdfcss\";
更改为:
var basepath = @"G:\some-other\directory\pdfcss\";
问题 2 的解答:
现在知道这一点我可以解决你的第二个问题了:
As a secondary question, what happens if it finds multiple CSS files at that base URI?
没什么,您仍然需要将链接插入 head 元素中的 html。如果不添加,您将没有任何 css!
问题 3 的解答:
确实:
Finally (sorry for the dump), if the HTML contains FQDN URLs to images, I'm assuming/hoping it will pull the images directly? In other words, I'm hoping I don't also have to store/write those images to the specified base URI?
您可以执行以下操作:
<img id="logo"
src="https://xxxxx.blob.core.windows.net/path/to-image-
logo.png" />
我正在试用 iText7 并尝试拼凑如何做事。似乎我可以放入一个基本 URI 来获取外部资源,我假设如果它找到一个 .css 它会应用它吗?我有一种特殊情况,我更容易将 CSS 作为字符串保存在内存中。我可以使用 HtmlConverter.convertToPdf() 并将 HTML 作为字符串传递而不是 CSS.
似乎很奇怪作为次要问题,如果它在该基本 URI 处找到多个 CSS 文件会怎样?
最后(抱歉转储),如果 HTML 包含图像的 FQDN URL,我 assuming/hoping 它会直接拉取图像吗?换句话说,我希望我不必 store/write 那些图像到指定的基本 URI?
谢谢。
更新: 我整理了一个快速演示。我发现它 将 下载具有完整 URL 的图像,这很棒。
但是,它似乎没有加载我指定文件夹中的 CSS 文件。代码:
StringBuilder sb = new StringBuilder(File.ReadAllText("demoHtml.html"));
// this folder, which is relative to the test .exe, contains a file called pdf.css
ConverterProperties props = new ConverterProperties().SetBaseUri("Content/Pdf");
FileStream fs = new FileStream("itext.pdf", FileMode.Create);
HtmlConverter.ConvertToPdf(sb.ToString(), fs, props);
和 CSS:
img {
max-width: 100%;
}
table {
vertical-align: top;
}
td ol {
-webkit-padding-start: 15px;
padding-left: 15px;
}
thead tr {
background: #aaa;
}
tr:nth-child(even) {
background: #eee;
}
问题 1 的解答:
I'm trying out iText7 and trying to piece together how to do things. It seems that I can put in a base URI to grab external resources which I'm assuming if it finds a .css it will apply that? I have a particular situation where it's easier for me to hold the CSS in memory as a string. It seems odd that I can use HtmlConverter.convertToPdf() and pass in HTML as a string but not CSS.
我花了很多时间寻找这个问题的解决方案。一切似乎都是正确的,我什至问了一个关于使用 CSS 文件的支持问题。与 itext5 (itextsharp) 相反,itext7 无法管理其中包含 space 的 url。
因此在本地测试路径如下:c:/Path to project/project/name/wwwroot/ 将不起作用(注意 spaces)
起初我没有注意到这一点,因为我以编程方式生成了指向我的 css 文件夹的路径:
var basepath = env.ContentRootPath + "\wwwroot\pdfcss\";
更改为:
var basepath = @"G:\some-other\directory\pdfcss\";
问题 2 的解答:
现在知道这一点我可以解决你的第二个问题了:
As a secondary question, what happens if it finds multiple CSS files at that base URI?
没什么,您仍然需要将链接插入 head 元素中的 html。如果不添加,您将没有任何 css!
问题 3 的解答:
确实:
Finally (sorry for the dump), if the HTML contains FQDN URLs to images, I'm assuming/hoping it will pull the images directly? In other words, I'm hoping I don't also have to store/write those images to the specified base URI?
您可以执行以下操作:
<img id="logo"
src="https://xxxxx.blob.core.windows.net/path/to-image-
logo.png" />