使用 dompdf 生成 PDF 时文本溢出表格

发布于 2024-08-20 21:05:10 字数 324 浏览 3 评论 0原文

替代文本

我正在使用 dompdf 生成一些 PDF,其中包含表格中的一些文本和图像。但是,如果文本中包含较大的 URL,则该 URL 会一直换行到行尾。所有的文本和URL都被包裹在一个固定宽度和高度的div中,但URL仍然溢出。

在浏览器中呈现的相同 HTML 似乎没问题。

有什么想法吗?

alt text

I am generating some PDFs with dompdf, which contains some text and images in a table. But if the text has a large URL in it, the URL wraps all the way to the end of the line. All the text and URL are wrapped in a div with fixed width and height, yet the URL still overflows.

The same HTML rendered in the browser seems to be OK.

Any thoughts?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

丘比特射中我 2024-08-27 21:05:10

我相信 DOMPDF 使用相当有限的字符集来确定如何分割行。现在它只在破折号或空格处分割一条线。因此,示例中的 URL 之类的内容将超出容器的宽度。 DOMPDF 只是不知道如何分解它。

从 dompdf v0.6.0 开始,您可以设置文本样式,以便在任何字符处断开单词,例如:

<span style="word-wrap: break-word;">http://example.com/really/long/.../url</span>

它不像在特定字符上断开那样干净(例如 /)。如果您能够轻松地破解代码,则可以更优雅地解决该问题。打开文本重花类并修改分割行的正则表达式。正则表达式如下所示:

preg_split('/([\s-]+)/u', $text, -1, PREG_SPLIT_DELIM_CAPTURE)

修改该代码以包含您认为有助于良好换行的任何额外字符。例如,如果您希望文本中包含极长的 URL,您可以将 URL 分解为 ?、& 甚至 /:

$words = preg_split('/([\s-\?\&\/]+)/u', $text, -1, PREG_SPLIT_DELIM_CAPTURE);

在 dompdf 0.6.1 中,可以在 dompdf/include/text_frame_reflower.cls.php 中找到正则表达式第 86 和 371 行。在即将发布的 0.7.0 中,可以在 dompdf/src/FrameReflower/Text.php 第 106 和 402 行中找到正则表达式。

修改正则表达式的缺点是,这将影响所有文本(不仅仅是 URL)。

I believe DOMPDF is using a fairly limited character set for determining how to split a line. Right now it only splits a line at a dash or a space. So something like the URL you have in your sample is going to run past the width of the container. DOMPDF just doesn't know how to break it up.

Starting with dompdf v0.6.0 you can style your text so that words are broken at any character, e.g.:

<span style="word-wrap: break-word;">http://example.com/really/long/.../url</span>

It's not as clean as breaking on a particular character (e.g. a /). If you're comfortable hacking the code you can work around the problem a little more elegantly. Open up the text reflower class and modify the regular expression that splits the line. The regular expression looks like the following:

preg_split('/([\s-]+)/u', $text, -1, PREG_SPLIT_DELIM_CAPTURE)

Modify that code to include whatever extra characters you think will make for a good line break. You might, for example, break URLs up on ?, &, or even / if you expect to have extremely long URLs in your text:

$words = preg_split('/([\s-\?\&\/]+)/u', $text, -1, PREG_SPLIT_DELIM_CAPTURE);

In dompdf 0.6.1 the RegEx can be found in dompdf/include/text_frame_reflower.cls.php lines 86 and 371. In the upcoming 0.7.0 the RegEx can be found in dompdf/src/FrameReflower/Text.php lines 106 and 402.

The drawback to modifying the RegEx is that this will affect all text (not just URLs).

终弃我 2024-08-27 21:05:10

打开文件 dompdf/include/text_frame_reflower.cls.php 并细化如下所示的行:

$words = preg_split('/([\s-]+)/u', $text, -1, PREG_SPLIT_DELIM_CAPTURE);

修改正则表达式以包含您认为有助于良好换行的任何额外字符。例如,如果您希望文本中包含极长的 URL,您可以将 URL 拆分为 ?&,甚至 / :

$words = preg_split('/([\s-\?\&\/]+)/u', $text, -1, PREG_SPLIT_DELIM_CAPTURE);

还将以下行替换

$words = array_flip(preg_split("/[\s-]+/u",$str, -1, PREG_SPLIT_DELIM_CAPTURE));

$words = array_flip(preg_split('/([\s-\?\&\/]+)/u', $str, -1, PREG_SPLIT_DELIM_CAPTURE));

Open up the file dompdf/include/text_frame_reflower.cls.php and fine the line that looks like the following:

$words = preg_split('/([\s-]+)/u', $text, -1, PREG_SPLIT_DELIM_CAPTURE);

Modify the regular expression to include whatever extra characters you think will make for a good line break. You might, for example, break URLs up on ?, &, or even / if you expect to have extremely long URLs in your text:

$words = preg_split('/([\s-\?\&\/]+)/u', $text, -1, PREG_SPLIT_DELIM_CAPTURE);

Also replace the following line

$words = array_flip(preg_split("/[\s-]+/u",$str, -1, PREG_SPLIT_DELIM_CAPTURE));

with

$words = array_flip(preg_split('/([\s-\?\&\/]+)/u', $str, -1, PREG_SPLIT_DELIM_CAPTURE));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文