18
require_once("function/dompdf/dompdf_config.inc.php");
$dompdf = new DOMPDF();
foreach($modules as $module){
   $output = "Hello " .$module['name'];
   $dompdf->load_html($output);
   $dompdf->render();
   $output_pdf = $dompdf->output();
   file_put_contents($dir . $name_modulo . ".pdf", $output_pdf);

}

Fatal error: Uncaught exception 'DOMPDF_Exception' with message 'No block-level parent found. Not good.'

Eliana
  • 395
  • 3
  • 10
  • 20
  • Possible duplicate of [DOMPDF, I cannot create two pdf at time](http://stackoverflow.com/questions/20210986/dompdf-i-cannot-create-two-pdf-at-time) – Jakub Matczak May 30 '16 at 09:14
  • @dragoste Thanks! Create a single DOMPDF instance into cycle works .. why? – Eliana May 30 '16 at 09:53
  • As mentioned in answer under the link above, the DOMPDF class doesn't clean up after itself properly. I don't know any details. – Jakub Matczak May 30 '16 at 10:14
  • @dragoste, since there isn't an (accepted) answer in the linked question maybe this should remain open and answered? – BrianS May 31 '16 at 14:08

12 Answers12

31

Late to this thread, but based on the post at https://github.com/dompdf/dompdf/issues/902, I was able to fix this issue by removing spaces between <html><head>, </head><body>, and </body></html>

So, instead of having properly formated html code like this:

<html>
<head>
...
</head>
<body>
...
</body>
</html>

I deleted all the new lines or spaces between the tags, now it looks like this :

<html><head>
...
</head><body>
...
</body></html>

And everything is hunky-dory again

Kinglish
  • 23,358
  • 3
  • 22
  • 43
20

dompdf folder > dompdf_config.custom.inc.php file > try to uncomment the line

define("DOMPDF_ENABLE_HTML5PARSER", true);

Also replace unsupported html5 tags & attributes with supported one, clear html errors for better result

linktoahref
  • 7,812
  • 3
  • 29
  • 51
Nimya V
  • 336
  • 3
  • 5
  • That line doesn't exist in my dompdf_config.custom.inc.php and adding it just throws in a new error about headers being already sent. Not sure if it's related or not since it says __autoload is deprecrated. – logicbloke May 04 '21 at 23:34
19

Just define

$dompdf->set_option('enable_html5_parser', TRUE);

I think it will fix the issue.

Or can edit the dompdf/dompdf_config.inc.php file go to line no 322 change

def("DOMPDF_ENABLE_HTML5PARSER", false);

to

def("DOMPDF_ENABLE_HTML5PARSER", true);
Prasant Kumar
  • 1,008
  • 12
  • 13
4

go to configuration file dompdf_config.custom.inc.php and uncomment define("DOMPDF_ENABLE_HTML5PARSER", true); and check, basically it requires html5 parser enabled in your configuration file.

Ram Kaushik
  • 341
  • 3
  • 4
3

Just uncomment define("DOMPDF_ENABLE_HTML5PARSER", true); this line from dompdf_config.custom.inc.php this file.

You can find this file for codeigniter in vendor/dompdf/dompdf this directory.

bharat
  • 1,762
  • 1
  • 24
  • 32
Debdip
  • 31
  • 1
3

If you have installed dompdf using composer require then defining the enable_html5_parser option using the set_option method works well if you don't want to modify the vendor folder

So add the following to each document that is creating an error

$dompdf->set_option('enable_html5_parser', TRUE);
Tim Wiel
  • 55
  • 11
2

For newer versions of Dompdf you can use this to enable the HTML5 parser:

use Dompdf\Options;

$options = new Options();
$options->set('isHtml5ParserEnabled', true);
$dompdf = new Dompdf($options);
Gavin
  • 7,544
  • 4
  • 52
  • 72
1

I don't know why the answer regarding ("DOMPDF_ENABLE_HTML5PARSER", true) was voted down either. Because I spent over 30 hours trying to figure out why this was happening in my website, until I came across this solution. So I enabled the "DOMPDF_ENABLE_HTML5PARSER" by setting it to "TRUE" as noted above. And immediately the error regarding "No block-level parent found" was resolved and my scripts worked. Maybe the 2nd answer was voted down because it duplicated a previous answer (but cut a guy a break, why don't you? Maybe he didn't see it..). Or maybe the "downvoter" has an attitude problem.

McAuley
  • 413
  • 1
  • 3
  • 13
0

I had the same problem with domPDF 0.8.3 on PHP 5.6 and I couldn't find neither dompdf_config.custom.inc.php nor dompdf_config.inc.php and didn´t work either; to solve my problem, I added body {display:block;} to my CSS and had not further problem.

0

I tried locating the white space or other spaces. didn't succeed, so I just Minimized the HTML file that is a PDF template it removed all the spaces, white spaces, etc worked for me

VNicholson
  • 25
  • 4
0

In my case, it is a bit different.

The pdf creation code was in the loop but the dom pdf object creation was above the loop. So when it is trying to create the pdf for the second time, I had seen the above error. To resolve it, each and every time I am creating a new object and it worked as expected.

Example:

(Code not working)

//Reference of Dompdf namespace
use Dompdf\Dompdf;
use Dompdf\Options;

//instantiate and use the Options class
$options    =   new Options();
$options->set('enable_html5_parser', true);

$dompdf     =   new Dompdf($options);
for($i = 0; $i < $count; $i++) {
  //Create PDF
  $dompdf->loadHtml($form_data);
  $dompdf->setPaper('A4', 'landscape');
  $dompdf->render();
  $output = $dompdf->output();
  file_put_contents($pdf_file_name, $output);
}

Working Code:

//Reference of Dompdf namespace
use Dompdf\Dompdf;
use Dompdf\Options;

//instantiate and use the Options class
$options    =   new Options();
$options->set('enable_html5_parser', true);

for($i = 0; $i < $count; $i++) {
  $dompdf     =   new Dompdf($options);
  //Create PDF
  $dompdf->loadHtml($form_data);
  $dompdf->setPaper('A4', 'landscape');
  $dompdf->render();
  $output = $dompdf->output();
  file_put_contents($pdf_file_name, $output);
}
Surya
  • 37
  • 6
  • I fail to see what the difference is between your 2 codes. – logicbloke May 04 '21 at 23:23
  • Without seeing the description and properly describing how you are commenting that there is no difference and how you downvote it. The Dompdf object was outside of the loop in not working code. In working code, it is moved within the loop. – Surya May 13 '21 at 13:30
  • I think there's no need to instantiate DomPdf multiple times unless you have different options being used. I was able to fix my issue with DomPdf by upgrading it. This is especially true if using PHP 7. – logicbloke May 14 '21 at 17:34
  • Versions will keep on upgrade all the time, You don't know which version i have used. Initially you said you are failed to see the difference. Now you are saying no need to instantiate DomPdf multiple times on upgraded version. Without knowing the version that i have used how can you down vote it. I am sure that you didn't check for each dompdf version. I have clearly mentioned that the single instantiation didn't work for me. If you comment or answer on a particular library or software after ages mostly there will be an updates on it, without knowing the version, how can you comment. – Surya May 15 '21 at 04:45
0

You can enable the HTML5 parser as mentioned above or you can update the dompdf library. I found updating the dompdf library from 6.2 to 8.2 got past this error BUT if you are using Drupal like I am there's a little bit of trickery you have to do to get Drupal to recognize the newer version of the library (see here: https://www.drupal.org/project/print/issues/3010637 )