DOM PDF仅生成1页pdf跳过剩余内容 [英] DOM PDF generate only 1 page pdf skip remaining content

查看:119
本文介绍了DOM PDF仅生成1页pdf跳过剩余内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码,该代码使用DOM PDF库将HTML内容转换为PDF文件.

I have the following code which uses DOM PDF Library for converting Html content to PDF file.

$dompdf = new DOMPDF();
$dompdf->set_paper(array(0,0,450,306));
$dompdf->load_html($html);
$dompdf->render();
$output = $dompdf->output();
file_put_contents($pdf_path, $output);

但是它会基于html内容生成2-3页. 我想将其限制为仅一页,而跳过其余内容.

But it generates 2-3 pages based on the html content. I wanted to limit it to single page only and skip the remaining content.

推荐答案

最简单的方法是渲染为图像. GD适配器使您可以指定要呈现的页面.

The easiest way is to render to image. The GD adapter allows you to specify the page you want to render.

dompdf 0.6.2或更早版本:将DOMPDF_PDF_BACKEND设置为"GD",它仅呈现文档的第一页.

dompdf 0.6.2 or earlier: set DOMPDF_PDF_BACKEND to "GD" which only renders the first page of the document.

dompdf 0.7.0或更高版本:$dompdf->set_option('pdfBackend', 'GD');.此版本可渲染所有页面,并允许您指定要输出/流式传输的页面(默认为发送第一页).

dompdf 0.7.0 or later: $dompdf->set_option('pdfBackend', 'GD');. This release renders all pages and allows you to specify the page to output/stream (default is to send the first page).

如果您真的仅需要将第一页呈现为PDF则比较困难.尽管在技术上仅可以使用dompdf做到这一点,但实际上我倾向于完全渲染PDF,然后使用外部库仅提取第一页.

If you really need to render only the first page to a PDF that's a bit more difficult. Though technically possible to do this only with dompdf I'd actually be inclined to fully render the PDF then use an external library to pull out just the first page.

例如,使用 libmergepdf ,您可以这样做:

For example, with libmergepdf you could do it like this:

use iio\libmergepdf\Merger;
use iio\libmergepdf\Pages;
use Dompdf\Dompdf;

$m = new Merger();

$dompdf = new Dompdf();
$dompdf->load_html('...');
$dompdf->render();
$m->addRaw($dompdf->output(), new Pages('1'));

file_put_contents('onepager.pdf', $m->merge());

(特定于dompdf 0.7.0,但代码与0.6.2几乎相同)

这篇关于DOM PDF仅生成1页pdf跳过剩余内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆