kirby
kirby

Reputation: 4041

DOMPDF not working

I am trying to take some html from a textarea and convert it to a pdf. I donwnloaded DOMPDF from https://github.com/dompdf/dompdf and wrote the code below. When I click submit I get this error: "Internal Server Error". (my webhost doesn't tell me which line it's one) (the name of this file is test2.php)

<?php
if (isset($_POST['submit'])) {
$content = $_POST['content'];
if (empty($content)){
    $error = 'write something';
}
else {
    include_once( 'dompdf/dompdf_config.inc.php' );
    $dompdf = new DOMPDF();
    $dompdf->load_html($content);
    $dompdf->render();
    $dompdf->stream('example.pdf');
}
}


?>
<!DOCTYPE html>
<head>

</head>
<body>
<?php
if(isset($error)){
echo $error;
}
?>
<form method="post" action="test2.php">
<textarea name="content" id="content">hello world</textarea><br>
<input type="submit" name="submit" value='submit'>
</form>
</body>
</html>

Upvotes: 0

Views: 16201

Answers (2)

Santhosh Nair
Santhosh Nair

Reputation: 21

I was struggled for more than one month to solve this. Finally I solved it. the solution is below.

<?php
require_once 'dompdf/autoload.inc.php';
// reference the Dompdf namespace
use Dompdf\Dompdf;
?>
<html>
  <head></head>
  <body>
    <h1>Sucess</h1>
  </body>
</html>
<?php

$html = ob_get_clean();
$dompdf = new DOMPDF();
$dompdf->setPaper('A4', 'portrait');
//$dompdf->setPaper('A4', 'landscape');
$dompdf->load_html($html);
$dompdf->render();
//For view
$dompdf->stream("",array("Attachment" => false));
// for download
//$dompdf->stream("sample.pdf");

?>

Upvotes: 2

userabuser
userabuser

Reputation: 443

I had a similar issue on a client's server when using DOMPDF for a project.

It's possible that you do not have the right level of error reporting configured with your installation of PHP.

At the top of your script place the following; error_reporting(E_ALL);

Example:

error_reporting(E_ALL);
if (isset($_POST['submit'])) {
$content = $_POST['content'];
if (empty($content)){
    $error = 'write something';
}
else {
    include_once( 'dompdf/dompdf_config.inc.php' );
    $dompdf = new DOMPDF();
    $dompdf->load_html($content);
    $dompdf->render();
    $dompdf->stream('example.pdf');
}
}

You should now see a more detailed message about the type of error received.

There could be issues with the HTML markup you are passing to the $dompdf->load_html($content); method or alternatively you might be experiencing memory related issues (exceeding your memory allowance).

Typically these errors will report themselves but again depending on your setup, reporting might be limited.

Upvotes: 0

Related Questions