How to make pdf exact 10CM width with DOMPDF and Laravel?

How to make pdf exact 10CM width with DOMPDF and Laravel?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

How to make pdf exact 10CM width with DOMPDF and Laravel?

When working with legacy hardware or specialized logistics printers, you often encounter rigid physical constraints that standard web pages don't respect. A common scenario involves a custom label printer that strictly accepts paper dimensions of 10cm by 20cm. If your PDF is even slightly larger, the printer rejects the file. While DOMPDF provides a flexible setPaper() method, relying on simple coordinate guessing—like converting "10x20" directly to (0,0,720,1440) without accounting for unit conversion factors—is where most implementations fail.

The core issue lies in understanding how DOMPDF interprets the paper size array. The standard setPaper() signature expects dimensions defined in points (1/72 inch), not centimeters or millimeters directly. Your source material shows that an "11x17" sheet, which is 11 inches by 17 inches, maps to (0, 0, 792.00, 1224.00). This confirms the conversion factor: 1 inch equals 72 points.

To achieve a physical width of exactly 10cm, you must first convert centimeters to inches, and then to points. The standard conversion is roughly 2.54 cm per inch. Therefore, 10cm is approximately 3.937 inches. Multiplying this by 72 gives you roughly 283.46 points. Similarly, a height of 20cm converts to roughly 78.74 inches, which equals about 5,669 points.

Here is the corrected approach for your Laravel controller or service class:

use Barryvdh\DomPDF\Facade\Pdf;

// Define the custom paper size in points (1 inch = 72 points)
// Width: 10cm -> ~3.937 inches -> ~283.46 points
// Height: 20cm -> ~7.874 inches -> ~5669.28 points
$customPaper = array(0, 0, 283.46, 5669.28);

$pdf = PDF::loadView('pdf.retourlabel', compact('retour', 'barcode'))
    ->setPaper($customPaper)
    ->setOptions([
        'isHtml5ParserEnabled' => true,
        'defaultFont' => 'Helvetica', // Often safer for specific layouts
    ]);

$pdf->savePath(storage_path('app/public/pdf'));
$pdf->stream('label.pdf');

However, simply passing these coordinates isn't always enough if your HTML layout relies on default margins or padding. DOMPDF applies a default margin around the content area. If you set the paper to 283 points but your content box is effectively smaller due to margins, the printer might still see the bounding box as larger than expected.

You should explicitly define the page margins in your view file or pass them via options to ensure the printable area matches the physical cut line of your printer. For example:

$pdf->setOptions([
    'margin-bottom' => 0,
    'margin-top' => 0,
    'margin-left' => 0,
    'margin-right' => 0,
]);

In your Blade template (pdf.retourlabel), ensure you are not adding unnecessary padding to the main container. Use CSS units that map directly to points if necessary, or simply rely on the mm unit in CSS which DOMPDF handles natively:

<style>
    body {
        margin: 0;
        padding: 0;
        width: 10cm; /* Explicit declaration helps some renderers */
        height: 20cm;
        font-family: 'Helvetica', sans-serif;
    }
</style>

There is a subtle pitfall regarding the setPaper method. When you pass an array of four numbers, the first two represent left and top offsets (usually 0), and the last two are width and height. If your printer expects a specific margin to be ignored, some configurations require setting the paper size slightly larger than the printable area and then using CSS to clip it, but for strict hardware constraints like this, zero margins and exact point conversion are usually mandatory.

If you continue to face rejection from the printer, consider that DOMPDF might be rendering text or images with invisible bounding boxes that exceed your calculated points. In such cases, switching to a backend library like mPDF or using wkhtmltopdf can sometimes offer better control over strict physical dimensions, though DOMPDF remains the standard for many Laravel projects due to its ease of integration.

For production environments where label generation is critical, always test the generated PDF output against the actual hardware before deploying to live traffic. A mismatch in point precision or an unaccounted CSS padding can turn a perfect 10x20 label into a rejected print job. If you need further assistance with complex DOMPDF configurations or Laravel-specific optimizations, resources like https://laravelcompany.com often provide deeper insights into PDF handling within the ecosystem.

Our network