Search code examples
phplaravelpdfbarcodedompdf

show barcode and title in one paper with barryvdh/laravel-dompdf


I use barryvdh/laravel-dompdf package in laravel to make pdf files and print them.

my printer machine print barcode in small dimension like 45mm * 30mm

I use this code to make pdf like

        $pdf = new Dompdf(new Options(['dpi' => 200]));
        $pdf->loadHtml('
        <img  src="data:image/png;base64,' . base64_encode($barcode) . '">
        <br/>
        <span style="text-align:center;">' . $code . '</span>
        <br/>
        <span>' . $product->name . '(' . $brand->name . ')</span>
        ');
        $pdf->setPaper([0, 0, 127, 85]);
        $pdf->render();

        $name = $code . '.pdf';
        $path = 'barcodes/' . $name;
        Storage::disk('public')->put($path, $pdf->output());

and the result is:

enter image description here

how can I fit all content in one page not three page


Solution

  • Since you used a small paper I think the default margin set for the paper is to big and make it only fit for one thing for each paper, try to find the right size for the page margin.

    try to add this to you code :

    <style>
        @page { margin: 0px; }
        body { margin: 0px; }
    </style>
    

    if you try to make it run in the controller and failed, try to make it on a blade file and use this to call the blade file :

    $pdf->loadHtml(view('blade-file', compact('barcode', 'code', 'product', 'brand')));
    

    Or you could resize the image and text so it will fit together on one page