简体   繁体   English

我想使用wkhtmltpdf和php将我的html页面的仅一部分转换为pdf

[英]I want to convert only some part of my html page to pdf using wkhtmltpdf with php

Actually i have converted html to pdf using url and static html page. 实际上,我已经使用url和静态html页面将html转换为pdf。 but now from my url or html page i dont want whole page to be pass for pdf conversion. 但是现在从我的url或html页面我不希望整个页面都通过pdf转换。 I want to pass only few content of my web page for html to pdf conversion. 我只想传递我网页的少量内容以将html转换为pdf。

suppose below is my html and i want to pass only 2'nd div for html to pdf conversion not whole html. 假设下面是我的html,我只想将html传递给pdf而不是整个html的2'div。 so how to pass only some part of html to convert. 所以如何只传递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>

following is my php code -- actually from one of my html page i am posting url to this php page and don't want whole web page to convert as pdf .i want only one div to be convert as pdf. 以下是我的php代码-实际上是从我的html页面之一中发布的url到此php页面,并且不希望整个网页都转换为pdf。我只希望将一个div转换为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)

Now i dont want whole content of web page for html to pdf conversion using wkhtmltopdf . 现在我不希望使用wkhtmltopdf将网页的全部内容从html转换为pdf。 I want only a particular DIV. 我只想要一个特定的DIV。

How about GETting the file, parsing it using some HTML parser , extracting the content you want and then feeding it to $pdf ? 如何获取文件, 使用一些HTML解析器对其进行解析 ,提取所需的内容,然后将其提供给$pdf呢? I don't know the wrapper that you use, but I'd think that's possible even though you don't use streams here. 我不知道您使用的包装器,但是我认为即使您不在此处使用流,这也是可能的。

If not, save the extracted part as a file and feed that file to $pdf . 如果不是,请将提取的部分另存为文件,然后将该文件提供给$pdf

There's a great answer on extracting parts of HTML in PHP here: https://stackoverflow.com/a/3577654/694325 在这里提取PHP中的HTML部分有一个很好的答案: https//stackoverflow.com/a/3577654/694325

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM