DomPDF Failed To Generate PDF from Simple HTML Boo

2019-09-19 19:12发布

问题:

I have this simple HTML file (simple.html) to test DomPDF :

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
    <title>Bootstrap 101 Template</title>

    <!-- Latest compiled and minified CSS -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">

    <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
      <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
  </head>
  <body>
    <h1>Hello, world!</h1>

    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <!-- Include all compiled plugins (below), or include individual files as needed -->
    <!-- Latest compiled and minified JavaScript -->
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>
  </body>
</html>

and here's my DomPDF generator file :

<?php
require_once '../../../lib/dompdf/autoload.inc.php';

use Dompdf\Dompdf;

// instantiate and use the dompdf class
$dompdf = new Dompdf();

$invoice = file_get_contents('invoice.html');
$dompdf->loadHtml($invoice);

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

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

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

?>

and here's the error message...

Warning: fopen(/home/***/lib/dompdf/lib/fonts/345658495770f44fa67e0e37c1b01eb4.ufm): failed to open stream: Permission denied in /home/***/lib/dompdf/lib/php-font-lib/src/FontLib/AdobeFontMetrics.php on line 45

Warning: fwrite() expects parameter 1 to be resource, boolean given in /home/***/lib/dompdf/lib/php-font-lib/src/FontLib/AdobeFontMetrics.php on line 191

why the PHP failed to open /lib/dompdf/lib/fonts folder? it already have drwxrwxr-x permission.

and why I don't see any 345658495770f44fa67e0e37c1b01eb4.ufm file there?

thank you

回答1:

I've tried by saving all the needed html in a variable $html and then using

$dompdf->loadHtml(html_entity_decode($html));

It should work when taking html from an external file, too. Try replacing your line

$dompdf->loadHtml($invoice);

with

$dompdf->loadHtml(html_entity_decode($invoice));


标签: php dompdf