Question

Actually i have converted html to pdf using url and static html page. but now from my url or html page i dont want whole page to be pass for pdf conversion. I want to pass only few content of my web page for html to pdf conversion.

suppose below is my html and i want to pass only 2'nd div for html to pdf conversion not whole html. so how to pass only some part of html to convert.

    <!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
    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 . I want only a particular DIV.

Was it helpful?

Solution

How about GETting the file, parsing it using some HTML parser, extracting the content you want and then feeding it to $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.

There's a great answer on extracting parts of HTML in PHP here: https://stackoverflow.com/a/3577654/694325

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top