实际上,我已经使用url和静态html页面将html转换为pdf。但现在从我的url或html页面,我不希望整个页面都通过pdf转换。我只想传递我的网页的一些内容,以进行html到pdf的转换。
假设下面是我的html,我只想传递2'nddiv用于html到pdf的转换,而不是整个html。那么如何只传递html的一部分进行转换。
<!DOCTYPE html>
<head>
</head>
<body>
<div id="print-area">
<div id="header">
This is an example header.
</div>
<div id="content">
<h1>Demo</h1>
<p>This is example content</p>
</div>
<div id="footer">
This is an example footer.
</div>
</div>
</body>
</html>
以下是我的php代码——实际上,从我的一个html页面,我正在将url发布到这个php页面,不希望整个网页转换为pdf。
<?php
require_once('WkHtmlToPdf.php');
$url = trim($_POST['textLink']);
$urlLink = "'$url'";
$date = date("Y-m-d");
$pdfNm = 'testPDF' . $date . '.pdf';
// Create a new WKHtmlToPdf object with some global PDF options
$pdf = new WkHtmlToPdf(array(
'use-xserver',
'no-outline', // Make Chrome not complain
'margin-top' => 0,
'margin-right' => 0,
'margin-bottom' => 0,
'margin-left' => 0,
));
// Set default page options for all following pages
$pdf->setPageOptions(array(
'disable-smart-shrinking',
'user-style-sheet' => 'css/pdf.css',
));
$pdf->addPage($urlLink); //we can also pass html file for conversion
//$pdf->addPage('demo.html');
//$pdf->saveAs('/tmp/new.pdf'); //Save as rather than sending it to user to save
$pdf->send($pdfNm)
现在我不想使用wkhtmltopdf将网页的全部内容转换为html到pdf。我只想要一个特定的分区。
获取文件,使用一些HTML解析器对其进行解析,提取所需内容,然后将其提供给$pdf
怎么样?我不知道你使用的包装器,但我认为这是可能的,即使你在这里不使用流。
如果没有,请将提取的零件另存为文件,并将该文件提供给$pdf
。
这里有一个关于在PHP中提取HTML部分的好答案:https://stackoverflow.com/a/3577654/694325