dejaya
October 10, 2024, 8:49am
1
Hey all …
I think this may be an issue with my config in MODX but it manifests itself in PDFResource.
I’m using PDFResource to automatically create a PDF version of a Resource when saved.
The page I’m testing is:
domain.xyz/insights/page
When saved, this creates a PDF at:
assets/pdf/insights/page.pdf
The PDF works great, except that images fail to load.
Using PDFResource’s debug mode I can see that it’s looking for the image at:
assets/uploads/images/image.jpg
If I swap that in dev tools to:
/assets/uploads/images/image.jpg
or ../assets/uploads/images/image.jpg
the image loads - suggesting that it’s trying to access the image path relative to the insights
folder.
But I don’t know how to affect that change in my config across MODX or PDFResource.
Some further info:
The images are stored and accessed via a Media Source “Uploads”
basePath: assets/uploads/
basePathRelative: Yes
baseUrl: assets/uploads/
baseUrlRelative: Yes
System setting use_alias_path
is set to Yes
MODX 3.0.5-pl
PDFResource 2.0.1-pl
PHP 8.3
I did try adding a /
to the beginning of the basePath and baseUrl in the Media Source - but that didn’t seem to change anything at all.
Any help much appreciated!
How do you output the images on the page?
With a path relative to the <base>
tag like this?
<head>
<base href="https://somedomain.com/">
...
</head>
<body>
<img src="my_image.jpg">
</body>
</html>
dejaya
October 10, 2024, 9:41am
3
Yes the <base>
is present just like your example.
The images in questions are in the [[*content]]
field and so are selected using the media browser & media source.
So the result on the standard resource is:
<head>
<base href="https://somedomain.com/">
...
</head>
<body>
<img src="assets/uploads/images/image.jpg">
</body>
</html>
This works fine - it’s only when PDFResource tries to recreate the page that the issue occurs.
Looking at the mPDF documentation for the function SetBasePath() , it says that
From mPDF 5.7 the HTML tag <base href="">
is parsed in the HTML and used to set the base path.
But testing the PDFResource extra, this doesn’t seem to be the case.
When I change the code though, and add the line
$this->pdf->setBasePath($this->modx->getOption('site_url'));
here (on line 289 before WriteHTML()
is called)
// Call additional mPDF methods
$mpdfMethods = json_decode($this->getOption('mPDFMethods', $pdfOptions, ''), true);
$mpdfMethods = (is_array($mpdfMethods)) ? $mpdfMethods : [];
foreach ($mpdfMethods as $methodName) {
$value = $this->getOption($methodName, $pdfOptions, '');
$value = (is_array($value)) ? $value : json_decode($value, true);
if ($value && method_exists($this->pdf, $methodName)) {
call_user_func_array([$this->pdf, $methodName], $value);
}
}
$this->pdf->WriteHTML($css, 1);
$this->pdf->WriteHTML($html, 2);
if (!is_null($aliasPath)) {
return $this->pdf->Output($this->getOption('pdfPath') . $aliasPath . $resource->get('alias') . '.pdf', 'F');
} else {
header('Content-Type: application/pdf');
header('Content-Disposition:inline;filename=' . $resource->get('alias') . '.pdf');
echo $this->pdf->Output('', 'S');
@session_write_close();
the images get added correctly to the PDF file.
I also tried altering the chunk “tplPDF” to include <base href="[[!++site_url]]">
in the <head>
, but that doesn’t seem to work.
dejaya
October 10, 2024, 10:13am
5
That’s got it!
I guess I’ll need to be careful with any future PDFResource updates - unless they include that fix.
Thanks as always.
Out of interest, what’s the general rule on the Media Source paths when use_alias_path
is enabled?
Should they have an opening /
?
I ask because the use_alias_path
system setting has the following info:
That stuff hurts my head sometimes.
Thanks again
I think it’s possible to call setBasePath()
without changing the code by setting the system setting pdfresource.mPDFMethods
to ["setBasePath"]
and adding a new system setting pdfresource.setBasePath
with the value ["https://somedomain.com/"]
. (The problem is, that the URL has to be hard-coded here.)
I’m probably missing something here. There has to be a simpler solution.
I’m not sure. When you use <base href="[[!++site_url]]">
in the template, I guess all paths can always be relative.
1 Like
system
Closed
October 12, 2024, 10:34am
7
This topic was automatically closed 2 days after discussion ended and a solution was marked. New replies are no longer allowed. You can open a new topic by clicking the link icon below the original post or solution and selecting “+ New Topic”.