1

I'm trying to generate PDF with content of a view in Laravel application using spatie/browsershot & laravel-browsershot wrapper; I am using svg as my letterhead background and would like to place content of the view in within certain area of the SVG on every page. Unfortunately the margins ->margins(40, 20, 40, 30) makes my #watermark div to shift with the margin - it is not in fixed 0,0 position anymore. Could you help me to setup the watermark div correctly please? So it is not affected by page margins

in controller

 return PDF::loadView('pdf.letter', compact('letter'))
            ->showBackground()
            ->waitUntilNetworkIdle()
            ->margins(40, 20, 40, 30)
            ->format('A4')
            ->inline();

in my blade I setup following css

#watermark { 
 position: fixed;
 top: 0px;
 left: 0px;
 width: 21cm;
 height: 29.7cm;
 z-index: -1000;
}
<body>
    <div id="watermark">
        <img src="{{ asset('/img/a4.svg') }}" height="100%" width="100%" />
    </div>
    <div>
        <!-- The content of PDF here -->
    </div>
</body>

The best what I achieved is this

here

where dash line rectangle is the area in the SVG file where I wish all content of the view, to flow in through all the pages, respecting set margins

I achieved it with removal of ->margins(....) in php and adding style to the <body> tag

body {
 margin-top: 4cm;
 margin-right: 2cm;
 margin-bottom: 4cm;
 margin-left: 3cm;
}

as you see the margin of the page seems to be 0 on the bottom of the 1st page and on top of next page

apokryfos
  • 38,771
  • 9
  • 70
  • 114
dascorp
  • 183
  • 7
  • 15

1 Answers1

2

Try the following:

Custom Header and Footer - https://github.com/spatie/browsershot#headers-and-footers

Browsershot::html($someHtml)
 ->showBrowserHeaderAndFooter()
 ->headerHtml($someHtml)
 ->footerHtml($someHtml)
 ->save('example.pdf');

and

Handle page breaks - https://github.com/spatie/browsershot/issues/333

<div style="page-break-after:always;">

or

This can be solved with <div style="page-break-inside: avoid;">. Just keep in mind the property doesn't work inside flex.

Kirk Beard
  • 9,569
  • 12
  • 43
  • 47
TravisHi
  • 21
  • 4
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. – Brian61354270 Apr 21 '20 at 00:38
  • Thanks Brian, I improved my answer in case the links disappear from the internet (I hate it when I find an answer to dead links, I totally forgot). – TravisHi Apr 21 '20 at 00:54
  • @TravisHi thank you for the answer, I did not expect my question being noticed after the long time from posting. The ` ->headerHtml($someHtml)` does nothing when I put simple html like `

    SomeText

    however if I don't chain `headerHtml` method standard chrome header is printed with the page print date in my case. Could you advice how $someHtml shall be structured to include said image?
    – dascorp May 03 '20 at 19:05
  • @dascorp for e.g. ~~~ ->headerHtml('

    header

    ') ~~~
    – TravisHi May 04 '20 at 22:22