0

My html to pdf conversion using wkhtmlpdf working fine without the <style> tag but when I include the style tag it's not working that means not generating the pdf some one help me to solve this issue or let me know how to include my style.css in this format.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Tijo John
  • 686
  • 1
  • 9
  • 20

1 Answers1

0

The way I handle this is as follows--

<?php
// create some HTML content
    $html = '<!DOCTYPE html><html><head>
    <style>
        body {
            margin:     0;
            padding:    0;
        }
        ...
    </style></head><body>
    <!-- Your body content -->
    </body></html>';

$options = array(
                "no-outline",
                "binary"                    => "/usr/local/bin/wkhtmltopdf",
                "margin-top"                => ".25in",
                "margin-right"              => ".25in",
                "margin-bottom"             => ".55in",
                "margin-left"               => ".25in",
                "zoom"                      => 1
            );

$footer = '<!DOCTYPE html><html><head></head><body>...</body></html>';

$page_options = array('footer-html' => $footer);
$pdf->addPage($html, $page_options);

$pdf->saveAs('MY_PDF_NAME.pdf');

Hope this helps!

Best,

-Rush

Rushikumar
  • 1,774
  • 5
  • 18
  • 28
  • Thank you but its not working if it possible to use that style in the same html – Tijo John Jun 06 '17 at 12:06
  • @Tijo what errors are you seeing? can you please output your log (first clear it and then run the script and then paste the output) – Rushikumar Jun 06 '17 at 12:13
  • no error displaying there its working fine without that style tag – Tijo John Jun 06 '17 at 12:16
  • use mikehaertl\wkhtmlto\Pdf; $binary = '/usr/bin/xvfb-run -- /usr/bin/wkhtmltopdf'; $page_options = array('user-style-sheet' => '/css/pdf.css',); $wkpdf = new Pdf(); $wkpdf->binary=$binary; $wkpdf->addPage($htm,$page_options); $wkpdf->saveAs('./report.pdf'); $wkpdf->getError(); – Tijo John Jun 06 '17 at 12:18
  • as soon as you add the inline ` – Rushikumar Jun 06 '17 at 12:24
  • but my variable $htm contains all inline css but it contains some images thats an issue? – Tijo John Jun 06 '17 at 12:55
  • try removing images... did you try echoing the `$html` (or `$htm`) variable? I have never had issue with phpwkhtmltopdf/wkhtmltopdf libs, using the structure I posted in my answer. Lastly, try removing parts of inline CSS... each time you remove some part of inline css, run the script and see if it works... – Rushikumar Jun 06 '17 at 14:21