【问题标题】:wkhtmltopdf div background colorwkhtmltopdf div背景颜色
【发布时间】:2014-08-04 12:08:07
【问题描述】:

我在我的亚马逊 ubuntu 实例上使用 wkhtmltopdf 来生成发票的 PDF。该页面是PHP。除了 html div 标签的背景颜色外,一切正常。边框颜色工作正常。 wkhtmltopdf 中是否有任何设置可以启用 div 标签的打印背景颜色?

我尝试过 bgcolor、css 和内联样式,我也检查过在表格结构中转换页面,但这些都没有帮助。

【问题讨论】:

  • 在使用 wkhtmltopdf 转换之前,HTML 输出中是否存在背景?
  • 是的,页面上有背景。
  • 你使用外部css文件吗?如果是这样,wkhtmltopdf 是否能够加载此样式表?

标签: php wkhtmltopdf


【解决方案1】:

我也遇到了这个问题,而且 css 就在同一个 HTML 文件中。对我来说,诀窍最终是在 background-color 标记末尾有点神秘的“!important”,如下所示:

background-color: #f2f4f7 !important;

【讨论】:

  • 嗨 sn-p 不工作你的意思是这里删除重要的还是添加它?
  • @shareef 添加,而不是删除。如果您有一个带有背景颜色样式的 div,但它没有出现在生成的 PDF 中,请尝试将 !important 添加到背景颜色声明中,这应该可以工作。很奇怪。
  • @DJGrossman 谢谢,stackoverflow.com/questions/34706302/… 似乎解决了我的问题
【解决方案2】:

特别是当您从 google App Script 生成 PDF 时使用它

@media print 
{
.class 
{
     background-color: #1a4567 !important;
     -webkit-print-color-adjust: exact;
}
}

【讨论】:

    【解决方案3】:

    如果您可以通过在 CSS 规则中添加 !important 来解决它 那么你有一个@media print 规则妨碍了

    例如,就我而言,我所包含的 CSS 规则:

    @media print {
        background: transparent !important; /* <= DISABLES backgrounds */
    }
    

    在许多其他带有!important@media print 规则中,我想为我的PDF 删除这些规则以获得目标HTML 的一些样式

    【讨论】:

      【解决方案4】:

      我遇到了同样的问题,边框颜色有效,但背景颜色无效。

      唯一对我有用的是直接在div 中设置background-color。例如:

      <div style="background-color: rgb(81, 130, 187) !important;"></div>
      

      不要忘记在末尾使用!important

      【讨论】:

        【解决方案5】:

        我遇到了同样的问题,但在我的情况下,解决方案是在 rgba 而不是十六进制中指定颜色。无需在 HTML 中指定。

        background-color: rgba(249, 203, 156, 0.71);
        

        而不是

        background-color: #F9CB9CB5;
        

        【讨论】:

          【解决方案6】:

          对于面临此问题的其他人无法通过接受的答案解决 - 您可能指定了以下无背景配置 -

          [
              'binary' => 'D:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe',
              'commandOptions' => [
                  'print-media-type',
                  'page-size' => 'Ledger',
                  'no-background'
              ]
          ]
          

          删除“无背景”配置,背景颜色会显示出来。

          【讨论】:

            猜你喜欢
            • 2018-05-04
            • 1970-01-01
            • 2011-09-05
            • 2012-02-28
            • 1970-01-01
            • 2013-08-20
            • 2015-02-21
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多