mercredi 24 avril 2019

Laravel Excel Images Displayed Incorrectly On First Page When Exporting To PDF (DOMPDF)

I am using Laravel Excel to generate excel and PDF downloads.
The excel download is completely fine, but the PDF export is not.

I am using DOMPDF to generate the PDFs I am including images into the file using the registerEvents method.
Sample code;

public function registerEvents(): array
{
    $items = $this->items;
    $stream_args = [
        "ssl" => [
            "verify_peer"=>false,
            "verify_peer_name"=>false
        ],
        "http" => [
            'timeout' => 60,
            'user_agent' => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.9) Gecko/20071025 Firefox/3.0.0.1'
        ]
    ];

    $img_height = Drawing::pixelsToPoints((543/445) * $this->image_width);
    $cell_height = Drawing::pixelsToPoints($img_height * 0.8);
    $cell_width = (Drawing::pixelsToPoints((543/445) * $img_height) / 7);

    return [
        AfterSheet::class => function(AfterSheet $event) use ($items, $stream_args, $img_height, $cell_height, $cell_width) {
            $worksheet = $event->sheet->getDelegate();

            // set page layout
            $worksheet->getPageSetup()
                ->setOrientation(PageSetup::ORIENTATION_LANDSCAPE)
                ->setPaperSize(PageSetup::PAPERSIZE_A4)
                ->setHorizontalCentered(true)
                ->setVerticalCentered(true);

            $worksheet->getPageMargins()
                ->setHeader(0.5)
                ->setFooter(0.5)
                ->setTop(0.25)
                ->setRight(0.25)
                ->setLeft(0.25)
                ->setBottom(0.25);

            $worksheet->getStyle('A1:H'.(count($items)+1))
                ->getAlignment()->setHorizontal(Alignment::HORIZONTAL_RIGHT);

            // set column widths
            $worksheet->getColumnDimension('A')->setWidth($cell_width);

            // add images
            foreach ($items as $index => $item) {
                $i = ($index+2);

                // set row height
                $worksheet->getRowDimension($i)->setRowHeight($cell_height);

                // add image
                if (! empty($item['image'])) {
                    $response = file_get_contents($item['image'], false, stream_context_create($stream_args));
                    $gdImage = imagecreatefromstring($response);

                    if ($gdImage) {
                        $drawing = new MemoryDrawing();
                        $drawing
                            ->setName($item['name'])
                            ->setDescription($item['name'])
                            ->setCoordinates('A'.$i)
                            ->setImageResource($gdImage)
                            ->setRenderingFunction(MemoryDrawing::RENDERING_JPEG)
                            ->setMimeType(MemoryDrawing::MIMETYPE_DEFAULT)
                            ->setHeight($img_height)
                            ->setWorksheet($worksheet);
                    }
                }
            }
        },
    ];

}

The images in the generated PDF are rendered small on the first page only. It make no difference to the number of images on the first page, they are always small, with pages two and above rendering the images as coded.

I have tried using the creating the download using the FromCollection, FromArray, FromView exportable concerns.
I am unable to use the MPDF rendering library as I hit the pcre.backtrace limit and the TCPDF library fails to export anything.

Versions used are;
"maatwebsite/excel": "3.1.3" "dompdf/dompdf": "^0.8.3"



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire