dompdf php to pdf

Asked

Viewed 2,985 times

4

I’m using the DomPDF to generate the pdf. The point is that I can’t convert my html to pdf. My html is generated by several lines of php with variables and etc...

I already tried passing a variable with everything to the DomPDF::loadhtml, but since there are so many lines of code,.

Is there any way to generate the final html and move to the variable ?

// include autoloader
require_once 'dompdf/autoload.inc.php';

// reference the Dompdf namespace
use Dompdf\Dompdf;

// instantiate and use the dompdf class
$dompdf = new Dompdf();
$dompdf->loadHtml('hello world');

// (Optional) Setup the paper size and orientation
$dompdf->setPaper('A4', 'landscape');

// Render the HTML as PDF
$dompdf->render();

// Get the generated PDF file contents
$pdf = $dompdf->output();

// Output the generated PDF to Browser
$dompdf->stream();

2 answers

3


Hello you can wear so:

$html = file_get_contents('http://www.seusite.com.br/seu_html_gigante.php');

require_once 'dompdf/autoload.inc.php';

use Dompdf\Dompdf;

$dompdf = new Dompdf();
$dompdf->loadHtml($html);
$dompdf->setPaper('A4', 'landscape');
$dompdf->render();
$pdf = $dompdf->output();
$dompdf->stream();

header('Content-type: application/pdf; charset=utf-8');
echo $pdf;

If you have problems with Session or something, consider using this other way:

ob_start();

include_once ("seu_html_gigante.php");

$html = ob_get_contents();

ob_end_clean()

require_once 'dompdf/autoload.inc.php';

use Dompdf\Dompdf;

$dompdf = new Dompdf();
$dompdf->loadHtml($html);
$dompdf->setPaper('A4', 'landscape');
$dompdf->render();
$pdf = $dompdf->output();
$dompdf->stream();

header('Content-type: application/pdf; charset=utf-8');
echo $pdf;
  • 1

    this Works...but now i have the problem that my page can only be Seen if im logged in so i get the loginpage Even if im allready logged

  • 1

    I changed to Suit your problem @I-amSam

1

Yes, there is. The way I know to "get the final html" is to give a include and capture it in output buffer.

Example:

ob_start();

include 'seu_html_gigante.php';

$dompdf->loadHtml(ob_get_clean());
  • tried.. but gave invalid code... php comes from wordpress... require_once 'dompdf/autoload.inc.php'; include 'www.toppromomkt.com/? wallet=janeiro-2016-2';


 // reference the Dompdf namespace
 use Dompdf\Dompdf;

 // instantiate and use the dompdf class
$dompdf = new Dompdf();
$dompdf->loadHtml(ob_get_clean());

// (Optional) Setup the paper size and orientation
$dompdf->setPaper('A4', 'Landscape'); // Render the HTML as PDF $dompdf->render(); // Get the generated PDF file Contents $pdf = $dompdf->output(); // Output the generated PDF to Browser $dompdf-stream>();

  • This mistake has nothing to do with what I posted. Maybe you have to ask another question.

  • I don’t really know how to do this... ...

  • The include you have to use is in your php file you’re looking to turn into pdf.

  • yes but I am using wordpress, it reads a php template to generate html , IE must be through the url of the category

Browser other questions tagged

You are not signed in. Login or sign up in order to post.