html2pdf,表格内容变淡。

huangapple go评论63阅读模式
英文:

html2pdf, table content is faded

问题

我尝试了html2pdf的v0.10.1和^0.9.3两个版本,当将表格导出为Pdf时都遇到了相同的问题。它的内容看起来有点淡,而表格的标题和边框看起来很好:

html2pdf,表格内容变淡。

这个问题令人感兴趣的是,只发生在一个表格上,所有其他数据和类似的表格都可以正常导出。以下是JavaScript代码:

var element = document.getElementsByClassName(elementClass)[0];
var opt = {
    margin: 1,
    filename: filename,
    image: { type: 'jpeg', quality: 0.98 },
    html2canvas: { scale: 2, useCORS: true },
    jsPDF: { unit: 'in', format: 'a4', orientation: isMobile ? 'portrait' : 'landscape' }
};
html2pdf().set(opt).from(element).outputPdf().then(function (pdf:any) {
    dotNetObj.invokeMethodAsync('FinishExporting');
}).save();

我是否漏掉了其他任何配置?非常感谢任何帮助!

英文:

I tried both versions of html2pdf v0.10.1 and ^0.9.3 and got the same issue when exporting the table to Pdf. It's content looks faded/muted, whereas the table header and borders look good:

html2pdf,表格内容变淡。

Interestingly, this happens only with one table, all other data and similar tables are exported fine.
Here's the JS:

 var element = document.getElementsByClassName(elementClass)[0];
    var opt = {           
        margin: 1,
        filename: filename,
        image: { type: 'jpeg', quality: 0.98 },
        html2canvas: { scale: 2, useCORS: true },
        jsPDF: { unit: 'in', format: 'a4', orientation: isMobile? 'portrait': 'landscape' }
    };
    html2pdf().set(opt).from(element).outputPdf().then(function (pdf:any) {
        dotNetObj.invokeMethodAsync('FinishExporting');
    }).save();

Am I missing any other configuration? Any help is much appreciated!

答案1

得分: 1

我已经创建了此GIST,其中包含一个可工作的示例。

您会注意到<style>标签,以确保表格行呈灰色条纹。您的PDF将相应地呈现。如果您删除样式标签,浏览器渲染的表格仍然会有条纹,但PDF中不会有。

我使用了Bootstrap来美化表格,并且一些Bootstrap的属性不被html2pdf识别,但它们被您的浏览器识别。

例如,Bootstrap为表格生成以下CSS:

.table {
    --bs-table-color-type: initial;
    --bs-table-bg-type: initial;
    --bs-table-color-state: initial;
    --bs-table-bg-state: initial;
    --bs-table-color: var(--bs-body-color);
    --bs-table-bg: var(--bs-body-bg);
    /* 等等等等 */
}

但Html2Pdf无法理解它。它只理解更简单的CSS。

您需要检查您正在使用的工具生成的CSS,并使用Google的开发者工具“Ctrl Shift I”进行测试,以查看是否可以使用更简单的CSS样式。

也许这个答案仍然无法解决您的问题。因此,您需要给我们提供这段代码的完整工作示例,就像我所做的那样,或者至少告诉我们您正在使用的工具。如果这个答案不能帮助您,请在负评之前告诉我更多信息。

英文:

I've created THIS GIST with a working example.

You will notice the <style> tag to ensure table rows are striped gray. Your pdf will be rendered accordingly. If you remove the style tag, your table rendered by the browser will still be striped, but not in your pdf.

I used bootstrap to make the table look good, and some properties of bootstrap are not recognized by html2pdf, but they are recognized by your browser.

For example, bootstrap generates the following css for the table:

.table {
    --bs-table-color-type: initial;
    --bs-table-bg-type: initial;
    --bs-table-color-state: initial;
    --bs-table-bg-state: initial;
    --bs-table-color: var(--bs-body-color);
    --bs-table-bg: var(--bs-body-bg);
    /* etc etc etc */
}

But Html2Pdf can't understand it. It understands simpler CSS.

You need to check the css generated of the tool you are using and play with google's developper's tools "ctrl shift i" to see if you can style with simpler css.

Maybe this answer will still not solve your issue. Thus, you need to give us a full working sample of this code, just like I did, or at least tell us what tools are you using. If this answer does not help you, please tell me a bit more before downvoting.

答案2

得分: 0

我最近遇到了类似的问题,当我尝试将页面保存为PDF时(当我尝试将它们打印为PDF时,一些元素看起来不同),我没有使用html2pdf,但问题看起来相似。

这一切都是由打印样式引起的。

我想建议检查CSS中的以下内容:

@media print

如果存在一些像不透明度、颜色、滤镜之类的CSS规则,那么这可能是问题的原因。

英文:

I had similar problem recently when I tried to save page as PDF (some elements looked differently when I tried to print them as PDF), I have not used html2pdf but problem looks similar.

It was all caused by print styles.

I would like to suggest checking css for:

@media print 

If there are some css rules like opacity, color, filter... Then it might be cause of this problem.

答案3

得分: -1

你可以设置backgroundColor属性来确保表格以正确的背景颜色渲染。

html2canvas: {
scale: 2,
useCORS: true,
backgroundColor: null // 将背景颜色设置为null或特定的颜色值
},

英文:

I think, you can set the backgroundColor property to ensure that the table is rendered with the correct background color.

   html2canvas: { 
    scale: 2,
    useCORS: true,
    backgroundColor: null  // Set the background color to null or a specific color value
},

huangapple
  • 本文由 发表于 2023年6月15日 16:01:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76480329.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定