Mencetak laporan ke printer menggunakan PHP memerlukan trik khusus. Ini dikarenakan mencetak sesuatu dari browser ke printer itu seringkali posisi layoutnya tidak sesuai seperti yang kita inginkan. Karena setiap browser memiliki setting margin, layout, skala yang bisa diatur-atur sendiri oleh pengguna browser di komputernya masing-masing.
Karena kondisinya demikian, maka kita tidak bisa mengandalkan pencetakan langsung ke halaman web — sebab layout bisa berubah-ubah. Solusinya adalah dengan mencetak ke .PDF. Sebab kalau sudah dicetak ke .PDF, maka di browser apapun akan mencetak sama persis seperti yang ada di file .PDF tersebut.
Salah satu library PDF untuk PHP yang menurut penulis cukup bagus untuk keperluan mencetak laporan adalah MPDF. Keunggulan MPDF ini adalah kemampuan mengkonversi format HTML menjadi PDF. Disamping itu ada fitur lain seperti penentuan ukuran kertas, setting header dan footer otomatis, dan sebagainya.
Library MPDF bisa di download gratis disini.
Di bawah ini ada contoh script sederhana bagaimana membuat tabel laporan menggunakan MPDF, yang nantinya bisa dicetak, bahkan disimpan dalam bentuk file .PDF
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | <?php include('./mpdf57/mpdf.php'); ob_start(); ?> <style> td { padding: 3px 5px 3px 5px; border-right: 1px solid #666666; border-bottom: 1px solid #666666; } .head td { font-weight: bold; font-size: 12px; background: #b7f0ff; } table .main tbody tr td { font-size: 12px; } table, table .main { width: 100%; border-top: 1px solid #666; border-left: 1px solid #666; border-collapse: collapse; background: #fff; } h1 { font-size:20px; } </style> <table class='main' repeat_header="1" cellspacing="0" width="100%" style="width:100%"> <thead> <tr class="head"> <td align='center' height="30">No.</td> <td>Kode Barang</td> <td align='center'>Nama Barang</td> <td align='right'>Saldo Stok</td> </tr> </thead> <tbody> <tr><td align='center'>1</td><td>PSG-001</td><td>Pasta Gigi</td><td align='right'>150</td></tr> <tr><td align='center'>2</td><td>SBM-002</td><td>Sabun Mandi</td><td align='right'>63</td></tr> <tr><td align='center'>3</td><td>PML-008</td><td>Pembersih Lantai</td><td align='right'>260</td></tr> </tbody> </table> <?php $content = ob_get_clean(); $header = '<table cellpadding=0 cellspacing=0 style="border:none;"> <tr><td style="border:none;" align="left"><img src="img/logo-disini.jpg" width="42"></td> <td width="100%" style="border:none;"><h1>Laporan Stok Barang</h1></td></tr></table>'; $footer = '<table cellpadding=0 cellspacing=0 style="border:none;"> <tr><td style="margin-right:-5px;border:none;" align="left"> Dicetak: '.date("Y-m-d H:i").'</td> <td style="margin-right:-5px;border:none;" align="right"> Halaman: {PAGENO} / {nb}</td></tr></table>'; try { $mpdf=new mPDF('utf-8', "A4", 9 ,'Arial', 5, 5, 20, 5, 5, 4); $mpdf->SetTitle("Laporan Stok Barang"); $mpdf->setHTMLHeader($header); $mpdf->setHTMLFooter($footer); $mpdf->WriteHTML($content); $mpdf->Output("laporan-stok.pdf","I"); } catch(Exception $e) { echo $e; exit; } ?> |
Contoh file .PDF yang dihasilkan dari script ini dapat dilihat disini.
Bila kita mengeset header dan footer dengan fungsi setHTMLHeader & setHTMLFooter, maka pada setiap halaman yang kita cetak, akan muncul header dan footer tersebut. Ini sangat berguna menampilkan judul laporan atau keterangan tambahan dibagian bawah halaman. Menjadikan laporan kita cantik dan rapi.
Bila anda ingin mengubah orientasi halaman menjadi Landscape di MPDF, caranya adalah dengan menambahkan “-L” pada ukuran kertas. Misalnya ingin menggunakan A4 Landscape, maka kodenya adalah “A4-L”. Untuk orientasi Potrait, biarkan saja tanpa tambahan apa-apa pada ukuran kertasnya.
1 | $mpdf=new mPDF('utf-8', "A4-L", 9 ,'Arial', 5, 5, 20, 5, 5, 4); |
Selamat mencoba.
Yang sedang trend:
- cara menggunakan mpdf
- solusi cetak halaman yang berantakan dengan php
- membuat laporan per tanggal di php menggunakan mpdf
- cara mengatur ukuran web pada html kertas a4
- bagaimana cetak print judul di php
- https://yandex ru/clck/jsredir?from=yandex ru;search;web;;&text=&etext=1840 IbuSAVnVWrtZsgEDE_4n4o-p_rkTIsJB3tGu4H5RJv0znB3sTw8k1iPvB9rLRFTh e25e7a5a5c621d86ea7d58ac30e4c9e9b9a0a82d&uuid=&state=_BLhILn4SxNIvvL0W45KSic66uCIg23qh8iRG98qeIXme
- https://yandex ru/clck/jsredir?from=yandex ru;search;web;;&text=&etext=1844 uCWK9pCHy34mf0d2NL1eHE5G06w61vJ73m2D3m6xI8i6mzhTY-Q3NcHP1vjOcwCI c6eed66cdec658fbc07c9d7fd66468e99d631080&uuid=&state=_BLhILn4SxNIvvL0W45KSic66uCIg23qh8iRG98qeIXme
- https://yandex ru/clck/jsredir?from=yandex ru;search;web;;&text=&etext=1839 N4yBZa1esR8ba_Mru487OT7RE7406Dr2B71O-u8OzQF9GrCWsNxxUEPo02tG1kVh 27fd0fae71eeaea4fdce874b65928b8fc909445f&uuid=&state=_BLhILn4SxNIvvL0W45KSic66uCIg23qh8iRG98qeIXme
- https://yandex ru/clck/jsredir?from=yandex ru;search;web;;&text=&etext=1838 y52VXbwPpoosAdY5Bk2Lpkrz-jww5i3O-pcaBDWcye9EiTNIpOwDDCocmkAYPD_Q f7288f64f83e3364306e41177918d841bbc478bd&uuid=&state=_BLhILn4SxNIvvL0W45KSic66uCIg23qh8iRG98qeIXme
- https://yandex ru/clck/jsredir?from=yandex ru;search;web;;&text=&etext=1836 EMxPgiF0nlUeq9D5SxjictZXoxBnQ34L2g3nuKdKv2Lhg2j3rb0xigGQM2JUd6li bb76711b98cffe690aa86f7093933ba1f2d49f7d&uuid=&state=_BLhILn4SxNIvvL0W45KSic66uCIg23qh8iRG98qeIXme
- https://yandex ru/clck/jsredir?from=yandex ru;search;web;;&text=&etext=1835 CBcFCYNIRRsJtGYltEoPJkjuwClhpqgVp5nBuYEu_uDn3AkmZ0BYMJAEdU8qIFIa 956eee40c8b9e7beb41d021ff6625437823569a1&uuid=&state=_BLhILn4SxNIvvL0W45KSic66uCIg23qh8iRG98qeIXme
- https://yandex ru/clck/jsredir?from=yandex ru;search;web;;&text=&etext=1834 iAAcGUCD3v2ydsiAqEXL2CmkUgZIRdA3FDjyH-h_rrUCOltKh3LqwEWIAJpL6WV6 54a6a9f7d324dfa8c8c4729e518e579336d92435&uuid=&state=_BLhILn4SxNIvvL0W45KSic66uCIg23qh8iRG98qeIXme
- https://yandex ru/clck/jsredir?from=yandex ru;search;web;;&text=&etext=1832 zBVucJ0AOrluReFD3dRx_YZelY2P5-f82jaUwI37g0k__zoOvSGEiROvlrujtPMN 493fe4038a8c2fe06e0508acbbc32249f182ef4a&uuid=&state=_BLhILn4SxNIvvL0W45KSic66uCIg23qh8iRG98qeIXme
- https://yandex ru/clck/jsredir?from=yandex ru;search;web;;&text=&etext=1829 XptKJTNuy73c2mTTXychQszml0zwzYFELb07gCywL4hxRGNYi48aTVUQSQpo0nz1 2e66a07ef01b0b89cb32726734bfb80ad1461f57&uuid=&state=_BLhILn4SxNIvvL0W45KSic66uCIg23qh8iRG98qeIXme
- https://yandex ru/clck/jsredir?from=yandex ru;search;web;;&text=&etext=1825 sTRjC1eOOTu6Hh8fkiPHRBWwT0vmXhfmExX_mV_dmq5oNBwQG9bMpxlu8tAzs_m8 da30a4c94b3273bcea1e6b2329f50a5fefe271dd&uuid=&state=_BLhILn4SxNIvvL0W45KSic66uCIg23qh8iRG98qeIXme
- https://yandex ru/clck/jsredir?from=yandex ru;search;web;;&text=&etext=1822 lWulbq6Sds6cBPEAFhENx5xkoupVmfdOsLCF3YogKK2Rs5KjzNwGg5TpyUohDtBJ 75232ae20600ebaea04737770b0c81032fb53780&uuid=&state=_BLhILn4SxNIvvL0W45KSic66uCIg23qh8iRG98qeIXme
- https://yandex ru/clck/jsredir?from=yandex ru;search;web;;&text=&etext=1820 MKBk39QPgzAJHcGHgM3jKcnK4hWSJA61I3e8aQ0tXadReXZpFgoN4R68m2073uZG 8c78a1485a60df45531d21185c6697150286ae84&uuid=&state=_BLhILn4SxNIvvL0W45KSic66uCIg23qh8iRG98qeIXme
- source code cetak laporan header dengan php
- cara mengatur Potrait dan landscape pada mpdf
- cara membuat landscape di mpdf/
Artikel menarik lainnya:
Hey would you mind letting me know which web host you’re working with?
I’ve loaded your blog in 3 completely different browsers and I
must say this blog loads a lot quicker then most. Can you suggest a good web hosting provider at a honest price?
Hello Will, I suggest to use web hosting service from A2 Hosting. http://www.a2hosting.com/8746.html
Greetings from Ohio! I’m bored at work so I decided to check out your site on my iphone
during lunch break. I really like the knowledge you provide here and can’t wait to
take a look when I get home. I’m shocked at
how quick your blog loaded on my phone .. I’m not
even using WIFI, just 3G .. Anyhow, superb blog!
I simply could not leave your web site prior to suggesting that I really loved the standard information an individual provide
in your visitors? Is gonna be back incessantly in order to check out new posts http://yahoo.co.uk
We have an entire generation of children spitting up on outfits that
price more than my month-to-month electrical bill.
I have been exploring for a little bit for any high-quality articles or blog posts on this kind of space .
Exploring in Yahoo I ultimately stumbled upon this site.
Studying this information So i am glad to convey that I’ve an incredibly good uncanny feeling I discovered just what I needed.
I so much no doubt will make certain to do
not fail to remember this web site and provides it a look on a constant basis.
Of course, printing reports to printers using PHP requires special tricks. This is because printing something from the browser to the printer is often the position of the layout is not appropriate as we want.
Yes, agree.
Hello admin, i must say you have very interesting articles here.
Your website can go viral. You need initial traffic only.
How to get it? Search for; Mertiso’s tips go viral