简体   繁体   中英

DOMXPath::query(): Invalid expression on Render

I've just started using dompdf (ex MPDF user) and it all worked in my dev environment, but as soon as I put it on my server it's randomly giving me the following error:

Type: ErrorException;
Message: DOMXPath::query(): Invalid expression;
File: /public_html/system/storage/vendor/dompdf/dompdf/src/Css/Stylesheet.php;
Line: 1034;

The line of this file is

$nodes = @$xp->query($query["query"]);

There is a similar issue from 2014 and I tried debugging using the code in the first post, but it didn't work.

I have all the required libraries, and I'm running PHP 7.3.23 with version 0.8.6 of dompdf.

The odd thing is if I use filp/whoops I don't get any errors showing, but if I don't the error above comes up.

I couldn't find out in the dompdf documentation how to debug, so any help would be much appreciated :)

The code that I'm putting in to render is available to view on my gist here , and my code I'm using to render it is as follows


Options = new Options();
$dompdfOptions->set('isRemoteEnabled', TRUE);
$dompdf = new Dompdf($dompdfOptions);
$context = stream_context_create([
  'ssl' => [
    'verify_peer' => FALSE,
    'verify_peer_name' => FALSE,
    'allow_self_signed' => TRUE
  ]
]);
$dompdf->setHttpContext($context);
$dompdf->loadHtml($compiledContent);
$dompdf->setPaper('A4', 'portrait');
$dompdf->render();
$dompdf->stream('po-000013', array('Attachment' => 0));

The cause of the error being raised is because Dompdf is producing an invalid XPath expression while parsing the stylesheet. The XPath query is prefixed with the @ error control handler in the code so that Dompdf can continue past these types of errors. More than likely your normal error handler is still raising errors even when error reporting is 0 (which is how the @ suppresses errors). filp/whoops, on the other hand, must be honoring the error reporting level.

The underlying issue, however, appears to be a problem parsing the Google Font URL. I found if you remove the semi-colon from the Google Font stylesheet reference Dompdf does not run into any parsing errors.

So, instead of this:

@import url('https://fonts.googleapis.com/css2?family=Open+Sans:wght@400;600;700&display=swap');

use this:

@import url("https://fonts.googleapis.com/css2?family=Open+Sans:wght@400&display=swap");
@import url("https://fonts.googleapis.com/css2?family=Open+Sans:wght@600&display=swap");
@import url("https://fonts.googleapis.com/css2?family=Open+Sans:wght@700&display=swap");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM