0

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>
kirby
  • 3,981
  • 14
  • 41
  • 54
  • You downloaded dompdf, did you also download [php-font-lib](https://github.com/PhenX/php-font-lib)? This library has to be installed with the latest code, but the ZIP download from github [does not include it](http://stackoverflow.com/a/13432787/264628). We're planning an update to our "nightly" download which is currently still pulling from the old SVN repository. – BrianS May 20 '13 at 14:55
  • @BrianS thanks, but how should I insert that library into my code? im not very good with this stuff – kirby May 20 '13 at 20:24
  • you can just download the ZIP from the php-font-lib project page (same as you did with dompdf). Then extract the files into your dompdf install at dompdf/lib/php-font-lib. If you continue having trouble you can [download the last released version from Google Code](http://code.google.com/p/dompdf/downloads/list). – BrianS May 20 '13 at 21:50

2 Answers2

1

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");

?>
MtwStark
  • 3,866
  • 1
  • 18
  • 32
0

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.

userabuser
  • 433
  • 5
  • 18
  • I put the error message at the top, but i still got the same error message from my webhost "Internal server error" – kirby May 19 '13 at 15:56