使文本出现在 PDF 文档中,但不可见

Make text present in PDF document, but invisible

如何让 HTML 文档中的文本在 PDF 文档中“存在”(通过 wkhtmltopdf 生成),但在查看或打印时不可见?

约束条件:

所以我想要一个 HTML 文档,在该文档中放置一个带有内联 CSS 样式的文本元素“FOO”,然后通过 wkhtmltopdf 处理它;然后,pdfgrep FOO 应该找到文本,文本位置和大小应该正确,但文本不应出现在 HTML 中,查看 PDF 时不出现,打印时不出现。

使用 PDF 的背景颜色(例如白色)为文本着色。

使用 color: transparent; 样式,文本在页面上将完全透明,但对于在文档中查找该文本的任何工具,它会正常显示。

我.

PDF语法支持不同的"text rendering modes"。这些允许 PDF 创建软件(或任何使用简单文本编辑器创建 PDF 的人)呈现任何文本,无论选择的字体如何,

  • 仅概述,
  • 只填满,
  • 勾勒并填充,
  • 既没有填充也没有轮廓(不可见),
  • 再加上一些处理裁剪的内容。

这是 PDF 规范中的插图:

但是,HTML 或 CSS(至少 AFAIK)不支持此功能。因此,以类似方式完成它的唯一选择是...

  • ...要么将 HTML 中的背景颜色设置为与文本颜色相同,
  • ...或将文本颜色设置为透明。

然后希望 htmltopdf 将其翻译成代表相同内容的 PDF...

二.

以下 HTML 代码(主要使用 style="color:transparent")对我有用。您必须决定它是否完全满足您的要求:

<html>
<head></head>
<body>
    <div style="color:transparent; background:red; border: 1px dashed currentColor;">
          The color of this text is transparent/invisible. <br />
          The background of this text is red.

          <div style="background:blue; height:9px;"></div>

          Above this text is a blue box with a height of 9px. <br />
          This block is surrounded by a transparent border.
    </div>
</body>
</html>

然后,当打开 PDF 时我无法阅读任何文本行,但我可以 select/mark/highlight 它们。

当运行

 pdftotext -layout my.html -

我看到以下文字:

The color of this text is transparent/invisible.
The background of this text is red.
Above this text is a blue box with a height of 9px.
This block is surrounded by a transparent border.