首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >我只想使用带php的wkhtmltpdf将html页面的一部分转换为pdf。

我只想使用带php的wkhtmltpdf将html页面的一部分转换为pdf。
EN

Stack Overflow用户
提问于 2013-11-25 13:00:34
回答 1查看 3.6K关注 0票数 1

实际上,我已经使用url和静态html页面将html转换为pdf。但现在,从我的网址或html页面,我不希望整个页面通过pdf转换。我只想通过我的网页的几个内容为html到pdf转换。

假设下面是我的html,我只想将html的第2‘div传递到pdf转换,而不是整个html。那么,如何只传递一部分html来进行转换。

代码语言:javascript
运行
复制
    <!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,.i只想将一个div转换为pdf。

代码语言:javascript
运行
复制
    <?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)

现在,我不希望整个网页内容的html到pdf转换使用wkhtmltopdf。我只想要一个特殊的设计资料。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-11-26 09:07:42

如何GETting文件,parsing it using some HTML parser,提取您想要的内容,然后提供给$pdf?我不知道您使用的包装器,但我认为这是可能的,即使您不使用流在这里。

如果没有,请将提取的部分保存为文件,并将该文件提供给$pdf

在PHP中提取部分HTML有一个很好的答案:https://stackoverflow.com/a/3577654/694325

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20193781

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档