Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

narwen's avatar

How to get a print view of a page?

How to get the print view of a page. Like I have a print link and when user presses there print view of the table is to be generated. Is there any solution?

0 likes
14 replies
narwen's avatar

@Penderis Thank You! for answering. But I went docs and did not understood it. Like what is the purpose of pdf.invoice and $data. I tried similar like it and got Non-static method Barryvdh\DomPDF\PDF::loadView() should not be called statically, assuming $this from incompatible context.

    $pdf = PDF::loadView('pdf.invoice', $data);
    return $pdf->download('invoice.pdf');
Penderis's avatar

@narwen

Route::get('pdf/invoice/{name}',function($name){
        $invoice = PDF::loadView('pdf.sample',array('name'=>$name));
//use which ever path you prefer to save the generated pdf, you do not need to save it you can stream or set for download directly.
      $invoice->save(storage_path().'/userFolders/uniquename.pdf');
    return $invoice->stream();
});
  1. PDF:: facade did you set this in your service providers alias?
  2. loadview() literally loads blade views and not pdfs. it generates the pdf from the view file and you can use blade.
  3. since load view will then create the pdf you can method chain seeing as this is now a object.
  4. Have a look at $name in the route you can pass variable from route or database as associative arrays to the pdf.

add test route to your routes file and maybe copy some random html to the correct view file. keep in mind for initial test maybe remove the save method line.

narwen's avatar

@Penderis I have included it in app.php both as service provider and alias. But still getting the same error: "Non-static method Barryvdh\DomPDF\PDF::loadView() should not be called statically, assuming $this from incompatible context".

here is my exact code:

        //routes.php

        Route::get('/transaction/pdf/{id}','TransactionController@getPdf');

    //TransactionController.php
      public function getPdf($id=null)
     {
            $invoice = PDF::loadView('pdf.pdf_bill',array('name'=>$id));
            return $invoice->stream();

        }
1 like
Penderis's avatar

Hi I set up my route and controller exactly the same and it works but I am using my own self contained view

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>Invoice</title>
    <link rel="stylesheet" href="style.css" media="all" />
  </head>
  <style>

  </style>
  <body>
    <header>
      <div id="logo">
        <img src="logo.png">
      </div>
      <div id="company">
        <h2 class="name">Company Name</h2>
        <div>455 Foggy Heights, AZ 85004, US</div>
        <div>(602) 519-0450</div>
        <div><a href="mailto:company@example.com">company@example.com</a></div>
      </div>
      </div>
    </header>
    <main>
      <div id="details" class="clearfix">
        <div id="client">
          <div class="to">INVOICE TO:</div>
          <h2 class="name">John Doe</h2>
          <div class="address">796 Silver Harbour, TX 79273, US</div>
          <div class="email"><a href="mailto:john@example.com">john@example.com</a></div>
        </div>
        <div id="invoice">
          <h1>INVOICE 3-2-1</h1>
          <div class="date">Date of Invoice: 01/06/2014</div>
          <div class="date">Due Date: 30/06/2014</div>
        </div>
      </div>
      <table border="0" cellspacing="0" cellpadding="0">
        <thead>
          <tr>
            <th class="no">#</th>
            <th class="desc">DESCRIPTION</th>
            <th class="unit">UNIT PRICE</th>
            <th class="qty">QUANTITY</th>
            <th class="total">TOTAL</th>
          </tr>
        </thead>
        <tbody>
          <tr>
            <td class="no">01</td>
            <td class="desc"><h3>Website Design</h3>Creating a recognizable design solution based on the company's existing visual identity</td>
            <td class="unit">$40.00</td>
            <td class="qty">30</td>
            <td class="total">$1,200.00</td>
          </tr>
          <tr>
            <td class="no">02</td>
            <td class="desc"><h3>Website Development</h3>Developing a Content Management System-based Website</td>
            <td class="unit">$40.00</td>
            <td class="qty">80</td>
            <td class="total">$3,200.00</td>
          </tr>
          <tr>
            <td class="no">03</td>
            <td class="desc"><h3>Search Engines Optimization</h3>Optimize the site for search engines (SEO)</td>
            <td class="unit">$40.00</td>
            <td class="qty">20</td>
            <td class="total">$800.00</td>
          </tr>
        </tbody>
        <tfoot>
          <tr>
            <td colspan="2"></td>
            <td colspan="2">SUBTOTAL</td>
            <td>$5,200.00</td>
          </tr>
          <tr>
            <td colspan="2"></td>
            <td colspan="2">TAX 25%</td>
            <td>$1,300.00</td>
          </tr>
          <tr>
            <td colspan="2"></td>
            <td colspan="2">GRAND TOTAL</td>
            <td>$6,500.00</td>
          </tr>
        </tfoot>
      </table>
      <div id="thanks">Thank you!</div>
      <div id="notices">
        <div>NOTICE:</div>
        <div class="notice">A finance charge of 1.5% will be made on unpaid balances after 30 days.</div>
      </div>
    </main>
    <footer>
      Invoice was created on a computer and is valid without the signature and seal.
    </footer>
  </body>
</html>

PS I am on Laravel 4.2 and imagine the issue might be if you on L5 , try app make or use the full namespace to refer to dompdf. view this maybe it helps http://stackoverflow.com/questions/29444164/html-to-pdf-convertor-in-laravel5

narwen's avatar

@Penderis Yes, I am in Laravel 5 and Barry has updated it to Laravel 5 but still getting the error!

Penderis's avatar

@narwen I do not want to cause you more problems if you find another method to output a print file go ahead, but as for debugging this start with something simple instead of loading a view first see if you can generate a simple pdf with loadHtml() as in the readme example to eliminate whether it is the view causing problems. , and have you dumped the composer autoload ?

narwen's avatar

@Penderis I tried with loadHTML() and it works but with loadView(), it send me back the error as above. I have dumped composer autoload too.

Penderis's avatar

@narwen then I believe the PDF facade is working. Now try to simplify your view then build it up again. just use a simple string in a view file and get that.

I tried now with some more complex views but stylesheets ect seem to need more work than I care to provide, I stripped out sheets and js but left includes to partials and still worked. I unfortunately cannot seem to recreate the issue.

PS do not try to dd() the object I did once and it crashed my VM.

narwen's avatar

@Penderis As you mentioned the PDF facade was not working, I tried with /PDF:: and it worked but it stripped out every bit of my styles!! Sad though

Penderis's avatar

@narwen if \PDF:: worked and PDF:: did not this is just that \PDF:: gets it from the global scope, think L5 everything is namespaced, as for the styles and if it is only for this particular use case inline css would be a option also no css3 , think of it as making something for email. Not the greatest usage of web tech but need to deliver it somehow.

forgive me as I may have over complicated things for you since you can also just have a print stylesheet. http://designshack.net/articles/css/6-thinks-i-learned-about-print-stylesheets-from-html5-boilerplate/

bashy's avatar
bashy
Best Answer
Level 65

Yeah the replies a few above are complicating the solution. Create a print CSS file and make a link to it so the browser initialises it.

I normally add a link with a class and do this once clicked

$('.print-window').click(function() {
    window.print();
});
<link href="/css/print.css" rel="stylesheet" media="print" type="text/css">
3 likes
andresramos's avatar

If you don't need styling within your PDF, use https://github.com/barryvdh/laravel-dompdf otherwise bashy solution is definitely way easier and much better. I struggled for several hours trying to print an html table from dompdf and the styling is just impossible.

It does not support flex css (I was using bulma framework). Also, it looks completely different in the html page than the pdf document, you have to add extra css (remove max-width td img css) if your printed page has images. Having width-mixed columns is a problem, does not support @section() @endsection so you have to add everything within the page.

Please or to participate in this conversation.