Insert image in pdf export (v2022)

This public forum is for user-to-user discussions of PHPMaker. Note that this is not support forum.
Post Reply
amsire2
User
Posts: 130

Insert image in pdf export (v2022)

Post by amsire2 »

Hi may I know on how to add an image in pdf export file. I try to used code below but the image cannot be found. I place the image in images folder.

// Page Exporting event
// $this->ExportDoc = export document object
function Page_Exporting()
{
global $basePath;
$basePath = BasePath(true);
$this->ExportDoc->Text .= "<p align='right'><img src='images/logo.png'></img><br><br><p>";
return true; // Return true to use default export and skip Row_Export event
}

this coding I place it at View Page - Page_Exporting


arbei
User
Posts: 9292

Post by arbei »

Make sure the path of the image is correct so that your image can be found. You may try absolute physical path.


philmills
User
Posts: 535

Post by philmills »

I was struggling with this too. My uploads folder is outside (above) web root for security reasons, and I have file path encryption turned on.

PDF exports had the image missing no matter what I did.
As soon as I moved the image into a location within web root and referenced that, the images were displayed.
Can I assume that ExportPDF is not able to pull images from outside (above) web root?


philmills
User
Posts: 535

Post by philmills »

I made some progress, in that encrypted images are now showing.

The problem is that I want an image from one of the fields to be shown in the header. File path is encrypted and I did a var dump and it looks like ["HrefValue"] is what I need.

I printed $this->myfield->HrefValue to check the URL, AND I can even click on the link it made in the pdf and the image opens. But I can only get the URL in the footer, I guess as the value is generated after the header.

However, the image still fails to display in the pdf - I wonder is it because dompdf can't recognise it as an image due to there being no file extension?
That wouldn't make much sense as the images which are part of my tables do show.

I now also managed to get non-encrypted images to show, but not with relative path.
It only works when I use the full path including domain with private $isRemoteEnabled = true; set in dompdf options which isn't very secure.

How can I get get both encrypted path and relative path images to display?


arbei
User
Posts: 9292

Post by arbei »

  1. Where did your encrypted file path come from? Since the image is embedded in PDF, why use encrypted file path?
  2. Make sure the relative path is correct (I think it should relative to the project folder).
  3. Use "/" as path separator, not "\", to avoid encoding issue.

philmills
User
Posts: 535

Post by philmills »

because the images are stored one level higher than site root, and are not accessible directly by URL. Yes the image is embedded in the PDF because its in the table, but I only added it to the table to get the var dump so I could try to figure out how to call the image from an encrypted path..
Its a passport style photo, and I want it above the tables along with name and other essential details in larger format. Everything else is fine apart from getting an encrypted photo to show. For now I have copied all the photos to a folder within site root and I'm using the full URL, so it's at least working now. But I really would rather not do that.

  • upload path is set to ** ../uploads/**
  • upload folder physical location is /var/www/uploads/,
  • site is root is at /var/www/html/
  • upload path for the field is "../uploads/".getSchoolID()."/StudentPhotos/"
  • uploads are working fine and into the right folder

I'm not using backslashes anywhere

I'm using Page_Exporting to format the pdf header, and as I said its fine apart from the encrypted photos.

I just tried adding the photo using $this->Photo->HrefValue to an img tag in Page_DataRendering and it works to the page, but doesn't show in the pdf


arbei
User
Posts: 9292

Post by arbei »

Assume your getSchoolID() works correctly in export mode, then using the path "../uploads/xxx/StudentPhotos/yourimage.png" in your HTML for export to PDF should work (for v2022). You better turn on Debug and log your built image path for debugging.


philmills
User
Posts: 535

Post by philmills »

Yes getSchoolID() works. I use it throughout the entire site. The path you gave doesn't won't work as my uploads folder is outside my site root, the browser cannot traverse to it. Debug didn't show anything at all relating to the building of the URL even after adding Log("Page Exporting") to the function. Though I echoed the URL in the PDF, and it shows as a relative path, all correct except that it can't be accessed due to it being outside root.

I had to modify ExportPdf.php so that detail table headings would show in the exported PDFs (IMO they should be shown by default)

ExportPdf

public function exportTableHeader()
    {
		$this->Text .= "<h2 style='font-family: Arial, Helvetica, sans-serif*; padding-top: 10px;'>" . $this->Table->tableCaption() . "</h2>"; // Add table caption
        $this->Text .= "<table class=\"ew-table\">\r\n";
    }

pageDataRendering code

    public function pageDataRendering(&$header)
    {
        // Example:
		//$header="";
		$studentID=$this->fk_StudentID->CurrentValue;
			$cleanname = str_replace(' ', '_', getStudentName($studentID));
			$teachername=getFullname(getClassTeacherID($studentID));
    		$header .= "<div class='row' style='max-width: 1000px;'>";
				$header .= "<div class='col'>";
				$header .= "<img src='".$this->C_StudentPhoto->HrefValue."' width='150' style='padding: 10px; padding: 10px; padding-right: 20px; margin-bottom: 5px; background: white; border: 1px solid gray; float: left;'>";
			$header .= "</div>";
			$header .= "<div class='col-6'>";
				$header .= "<h3 class='text-dark' style='font-family: Arial, Helvetica, sans-serif'><b>Dev Card</b> | <span style='color: gray'>".getStudentName($studentID)." | ".ltrim(getStudentClassParallelFromID($studentID), "0")."</span></h3>";
				$header .= "<h3 style='font-family: Arial, Helvetica, sans-serif'>".Language()->phrase('General_PersonalCode').": ".$studentID."</h3>";
				$header .= "<h3 style='font-family: Arial, Helvetica, sans-serif'>".Language()->phrase('AnnualDev_22').": ".$teachername."</h3>";
			$header .= "</div>";
			$header .= "<div class='col-4' text-align: right'>";

				if (!$this->isExport()) { //add download button, but hide it in exported file
				   $header .= "<div style='max-width: 125px; float: right;  text-align: center' class='align-right'><a class='btn btn-primary align-middle' role='button'  href='../".CurrentPageName()."/".$this->id->CurrentValue."?export=pdf' title='".Language()->phrase('ExportToPdfText')."'>&nbsp;".Language()->phrase('DownloadBtn')."</a><br>";
				   $header .= "<br><p class='text-danger align-middle'>To view the full card, laadi alla</p></div>";
				//}
				}

			$header .= "</div>";
    }

arbei
User
Posts: 9292

Post by arbei »

  1. The image is loaded by dompdf on the server side, it does not go through the browser at all.
  2. Be reminded that dompdf has a chroot setting for security, you need to add your path to it so it is allowed.

philmills
User
Posts: 535

Post by philmills »

Is it possible to set this option in Global Code ?
Or do I have to manually edit options.php in dompdf/src folder?

Just trying to avoid accidentally overwriting it

if i set $chroot ="/my/path" in dompdf options i get this error:

src/ExportPdf.php(130): Undefined constant "Dompdf\enabled"

line 130 is:

 $options = new \Dompdf\Options(self::$Options);

I couldn't figure this out, for now I'm using a subfolder within the the site root, but still looking for a solution.


arbei
User
Posts: 9292

Post by arbei »

  1. There is a static property Options in the ExportPdf class, you may get the options by, e.g. new \Dompdf\Options(ExportPdf::$Options), and get/set chroot by the getChroot() and setChroot() methods, see dompdf Options.php.
  2. The "chroot" option is an array.

philmills
User
Posts: 535

Post by philmills »

yep i found it

$options->setChroot(['/var/www/uploads/']);

using this in combination with an absolute path in the image URL worked:

<img src="/var/www/uploads/".getSchoolID()."/StudentPhotos/".StudentPhoto(thisstudent).">

Very happy i finally go this working

Thanks for pointing me in the right direction!


philmills
User
Posts: 535

Post by philmills »

But how can I override $options->setChroot(['/var/www/uploads/']); ?
I'd like to be able to drop this into a switch in Gobal Code based on IP address, so that I don't have to manually edit ExportPdf.php


philmills
User
Posts: 535

Post by philmills »

is there a public function i can use to encrypt the file path now that its working?

looks to me like i could use this somehow, but i don't understand

<?= GetFileViewTag($Page->C_StudentPhoto, $Page->C_StudentPhoto->getViewValue(), false) ?>

arbei
User
Posts: 9292

Post by arbei »

arbei wrote:

There is a static property Options in the ExportPdf class


philmills
User
Posts: 535

Post by philmills »

I saw that you wrote this earlier, but I can't make any sense of what the syntax should look like without an example. Still learning...


arbei
User
Posts: 9292

Post by arbei »

You may read PHP docs on Static properties.


Post Reply