Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ilernet's avatar

I can't print a pdf with dompdf in laravel

I have installed the dompdf package for laravel and when trying to create a pdf file I get characters in the browser, such as these

https://i.stack.imgur.com/8jcjl.png

Code controller

    public function downloadPdf($id)
    {

        $data = ['title' => 'Laravel 7 Generate PDF From View Example Tutorial'];
        $pdf = PDF::loadView('pdf', $data);

        return $pdf->download('Nicesnippets.pdf');

    }

Route api

Route::get('/forms/downloadPDF/{id}','Front\FormController@downloadPDF')->name('front.user.forms.downloadPdf');
0 likes
2 replies
gokhancelebi's avatar

I am using like this and it works for me

    $html = view("templates.pdf", compact("details", "projects", "staffs"));

    $pdf = \App::make('dompdf.wrapper');

    $pdf->loadHTML($html);


    $output = $pdf->output();

    $name = public_path("/pdf/ILA_PIF_NL".rand(0,10000).".pdf");

    file_put_contents($name,$output);

    return response()->download($name)->deleteFileAfterSend();

you can improve this using storage to save file instead of using file_put_contents , i am too lazy for that :)

ilernet's avatar

An important fact is that I work with docker.

My dockerfile is this

FROM php:7.4.1-apache

USER root

WORKDIR /var/www/html

RUN apt-get update && apt-get install -y \
        libpng-dev \
        zlib1g-dev \
        libxml2-dev \
        libzip-dev \
        libonig-dev \
        zip \
        curl \
        unzip \
        vim \
        fonts-liberation \
        fonts-liberation2 \
    && docker-php-ext-configure gd \
    && docker-php-ext-install -j$(nproc) gd \
    && docker-php-ext-install pdo_mysql \
    && docker-php-ext-install mysqli \
    && docker-php-ext-install zip \
    && docker-php-source delete


COPY .docker/vhost.conf /etc/apache2/sites-available/000-default.conf

RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

RUN chown -R www-data:www-data /var/www/html \
    && a2enmod rewrite

Please or to participate in this conversation.