0

I am using Laravel with DomPDF and it usually works well, but there is a case when it inserts blank pages (page after page), like pdf file, how to fix it pls.

I'm generating a pdf document using dompdf 0.6.0, and have a strange issue where a blank page is being created at the end. My (simplified) html:

Download PDF to preview

<!doctype html>
<html lang="en">
<head>
    <base href="{{ env('APP_URL') }}">
    <meta name="csrf-token" content="{{ csrf_token() }}">
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>

</head>
<style>
    .sheet {
        position: relative;
    }
    .row{
        clear: both;
        display: block;
    }

    .item {
        position: relative;
        float: left;
        display: inline;
        width: {{ $output_width }}mm;
        height: {{ $output_height }}mm;
    }

    .item img {
        position: absolute;
        width: 100%;
        height: 100%;
    }

    .elm-serial,
    .elm-username,
    .elm-password {
        position: absolute;
    }
    @page{margin:0}body{margin:0}.sheet{margin:0;overflow:hidden;position:relative;box-sizing:border-box;page-break-after:always}body.A3 .sheet{width:297mm;height:419mm}body.A3.landscape .sheet{width:420mm;height:296mm}body.A4 .sheet{width:210mm;height:296mm}body.A4.landscape .sheet{width:297mm;height:209mm}body.A5 .sheet{width:148mm;height:209mm}body.A5.landscape .sheet{width:210mm;height:147mm}body.letter .sheet{width:216mm;height:279mm}body.letter.landscape .sheet{width:280mm;height:215mm}body.legal .sheet{width:216mm;height:356mm}body.legal.landscape .sheet{width:357mm;height:215mm}.sheet.padding-10mm{padding:10mm}.sheet.padding-15mm{padding:15mm}.sheet.padding-20mm{padding:20mm}.sheet.padding-25mm{padding:25mm}@media screen{body{background:#e0e0e0}.sheet{background:#fff;box-shadow:0 .5mm 2mm rgba(0,0,0,.3);margin:5mm auto}}@media print{body.A3.landscape{width:420mm}body.A3,body.A4.landscape{width:297mm}body.A4,body.A5.landscape{width:210mm}body.A5{width:148mm}body.legal,body.letter{width:216mm}body.letter.landscape{width:280mm}body.legal.landscape{width:357mm}}

</style>
<body class="A4">
@php
    $counter = 0;
    $can_print_card = true;
@endphp
@for($p = 0 ; $p < $pages_count; $p++)
    <section class="sheet padding-10mm">
        @for($i = 0 ; $i < $rows_count; $i++)
            <div class="row">
                @for($j = 0 ; $j < $number_of_items_per_row; $j++)
                    @if($can_print_card)
                        <div class="item">
                            <img src="{{ $img }}" alt=""/>
                            <div class="elm-serial"
                                 style="display: {{ $card_component == 'serial_number' && $visibility == 'hidden' ? 'none' : '' }}; left: {{ $serial_output_x }}mm; top:{{ $serial_output_y }}mm; color: {{ $serial_output_color }}; font-size: {{ $serial_output_size }}">
                                {{ $info[$counter]->serial_number }}
                            </div>
                            <div class="elm-username"
                                 style="display: {{ $card_component == 'username' && $visibility == 'hidden' ? 'none' : '' }}; left: {{ $username_output_x }}mm; top:{{ $username_output_y }}mm; color: {{ $username_output_color }}; font-size: {{ $username_output_size }}">
                                {{ $info[$counter]->username }}
                            </div>
                            <div class="elm-password"
                                 style="display: {{ $card_component == 'password' && $visibility == 'hidden' ? 'none' : '' }}; left: {{ $password_output_x }}mm; top:{{ $password_output_y }}mm; color: {{ $password_output_color }}; font-size: {{ $password_output_size }}">
                                {{ $info[$counter]->username }}
                            </div>
                        </div>
                    @endif
                    @php
                        $counter++;
                    @endphp
                        @php
                            if($counter == $cards_count)
                            {
                                $can_print_card = false;
                            }
                        @endphp
                @endfor
            </div>
        @endfor
    </section>
@endfor
</body>
</html>
public function export_pdf(Request             $request,
                           CardGroupRepository $cardGroupRepo,
                                               $id)
{
    $id = $this->decrypt($id);
    /**
     * @var CardGroup $info
     */

    $card_component = $request->get('card_component');
    $visibility = $request->get('visibility');
    $color_serial = $request->get('color_serial');
    $color_username = $request->get('color_username');
    $color_password = $request->get('color_password');
    $size_serial = $request->get('size_serial');
    $size_username = $request->get('size_username');
    $size_password = $request->get('size_password');

    $positions = $request->get('positions');
    $positions = json_decode($positions, true);

    parent::$data['card_component'] = $card_component;
    parent::$data['visibility'] = $visibility;
    parent::$data['width'] = 190;
    parent::$data['height'] = 276;
    $page_with = 190;
    $page_height = 276;

    $number_of_items_per_row = parent::$data['number_of_items_per_row'] = intval($request->get('number_of_columns'));
    $img = parent::$data['img'] = asset($request->get('image'));

    $data = getimagesize($img);
    $width = $data[0];
    $height = $data[1];

    $output_width = parent::$data['output_width'] = intVal($page_with / $number_of_items_per_row);
    $output_height = parent::$data['output_height'] = $height * $output_width / $width;

    $rows_count = parent::$data['rows_count'] = intVal($page_height / $output_height);

    $serial_x = intVal(str_replace('px', '', $positions['serial']['x']));
    $serial_y = intVal(str_replace('px', '', $positions['serial']['y']));

    $serial_output_x = parent::$data['serial_output_x'] = $serial_x / $width * $output_width;
    $serial_output_y = parent::$data['serial_output_y'] = $serial_y / $height * $output_height;

    $username_x = intVal(str_replace('px', '', $positions['username']['x']));
    $username_y = intVal(str_replace('px', '', $positions['username']['y']));

    $username_output_x = parent::$data['username_output_x'] = $username_x / $width * $output_width;
    $username_output_y = parent::$data['username_output_y'] = $username_y / $height * $output_height;

    $password_x = intVal(str_replace('px', '', $positions['password']['x']));
    $password_y = intVal(str_replace('px', '', $positions['password']['y']));

    $password_output_x = parent::$data['password_output_x'] = $password_x / $width * $output_width;
    $password_output_y = parent::$data['password_output_y'] = $password_y / $height * $output_height;

    $serial_output_color = parent::$data['serial_output_color'] = $color_serial;
    $username_output_color = parent::$data['username_output_color'] = $color_username;
    $password_output_color = parent::$data['password_output_color'] = $color_password;

    $serial_output_size = parent::$data['serial_output_size'] = $size_serial;
    $username_output_size = parent::$data['username_output_size'] = $size_username;
    $password_output_size = parent::$data['password_output_size'] = $size_password;

    $file = public_path('2022-06-18.pdf');
    $info = parent::$data['info'] = DB::select(DB::raw('select serial_number,username,password from prepaid_cards limit 60'));
    $cards_count = parent::$data['cards_count'] = count($info);
    $pages_count = parent::$data['pages_count'] = ceil($cards_count / ($rows_count * $number_of_items_per_row));

    $pdf = Pdf::loadView('cards::admin.card_groups.export.prepaid_card_html', parent::$data);//->setWarnings(false);
    $customPaper = array(0,0,609.4488,1500);
    $pdf->save($file);

    echo "<a href='/2022-06-18.pdf'>download</a>";
}
RedOne
  • 33
  • 1
  • 1
  • 8
  • your counter on `$pages_count = parent::$data['pages_count'] = ceil($cards_count / ($rows_count * $number_of_items_per_row));` it's really correct? – David Silva Jun 28 '22 at 12:28
  • yes, there is not problem in code php Laravel, the problem just when export pdf – RedOne Jun 28 '22 at 12:33
  • The file is exported successfully, but prints one page and leaves the other blank, and so on, I mean if the file is 20 pages then it becomes 40 pages because one blank page is incremented after each page – RedOne Jun 28 '22 at 12:34
  • just a quick thought, if the page size is larger than allowed, does it create another one to compensate? Would it make sense to test smaller sizes? – David Silva Jun 29 '22 at 11:18
  • The page I installed is only A4, and I specify how many columns on the page and the number of cards, and the rest of the cards are repeated – RedOne Jun 29 '22 at 12:57
  • Are you did see this topic? maybe has some idea for us https://stackoverflow.com/a/21038884/6394559 – David Silva Jun 29 '22 at 16:44

0 Answers0