Search code examples
phplaravelpdfwkhtmltopdf

Wkhtmltopd how to set full width of pdf page


In Laravel 10 I'm using wkhtmltopd library to generate a PDF document.

The problem is when I try generate content in full width of the document, top and sides.

Some reason there is always white border around content.

enter image description here

This is my html code that I use to generate document:

<!DOCTYPE html>
<html style="margin: 0; padding: 0; background-color: #fd7e14">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Title</title>
<link rel="stylesheet" media="screen" href="https://fontlibrary.org//face/open-sauce-one" type="text/css"/>
<meta name="author" content="name author">
<style media="all">
    html {
        margin: 0 !important;
        padding:0 !important;
        border:0 !important;
        outline:0 !important;
    }
    body {
        margin: 0 !important;
        padding:0 !important;
        border:0 !important;
        outline:0 !important;
    }
</style>
</head>

<body>
   Test Content
</body>
</html>

Is there something wrong with styles?

Thank you.


Solution

  • Your html looks ok to me, but you also need to set all margins to 0 for the PDF document (in addition to the html content). The options for wkhtmltopdf on the command line are as follows:

    wkhtmltopdf -T 0 -B 0 -L 0 -R 0 <input_url> <output_file>
    

    It depends on the wrapper used how to set those options in PHP.

    See the manual for further explanations on all options.