phpsymfonydompdf

How to show image in generated pdf


enter image description hereI am genearting a pdf file using DOMPDF. My pdf file is admit card which holds the information about the particular student.

In this admit card I have wo images to show. So in my twig file,

Profile Image:

<img src="{{ response['display_image'] }}" alt="Profile Pic" width="100px" height="100px" class="img-thumbnail" />

Company Image:

<img src="images/webkul.png" alt="Webkul"/>

Both of these images are not generated in my PDF file. I gives 'image not found or type unknown'.

I am fetching profile image from database while company image is stored in project file that is,

public/assets/images/xyz.png

Controller:

$pdfOptions = new Options();

        $pdfOptions->set('defaultFont', 'Arial');
        $pdfOptions->setIsRemoteEnabled(true);
        $dompdf = new Dompdf($pdfOptions);
        $html = $this->renderView('download_pdf/index.html.twig', [
            'title' => "Admit-Card",
            'response' => $response,
            'data' => $data,
        ]);

        $dompdf->loadHtml($html);
        $dompdf->setPaper('A4', 'portrait');
        $dompdf->render();

I have attached the screen-shot.

How should I give path in my twig file so that image can be rendered in PDF file?


Solution

  • Actually problem exists in the header part of HTML file. I was extending head of my project which was containing lots of CSS and JS. I simply removed the head and used a simple and clean head instead and my problem was solved.

    Note: Do not extender header/footer of your project into Html of pdf file, instead use a new one for the same.