How to generate PDF in PHP 7.0.30?

Asked

Viewed 1,157 times

0

Good morning, I found several examples on the internet of how to generate reports with php and mysql, but in the examples everyone uses the php-5 and trying to do the same with the php-7.0.30 I couldn’t make the mistake (HTTP ERROR 500).

I tried using the library mPDF(which was already hard to find version 5.7 to download because on the official website I could not download.

Then how to generate pdf with php 7.0.30?

This is the code I’m using:

<?php
include ('pdf/mpdf.php');

$pagina = 
        "<html>
            <body>
                <h2>Relatório de Profissionais S Network</h2>
                <ul>
                    <li>teste</li>
                    <li>teste</li>
                    <li>teste</li>
                    <li>teste</li>
                </ul>
                <h4>Fonte: http://www.snetwork.com.br</h4>
            </body>
        </html>
        ";


$arquivo = "Profissionais01.php";

$mpdf = new mPDF();
$mpdf->WriteHTML($pagina);

$mpdf->Output($arquivo, 'I');


?>

Note: I don’t use any framework (Laravel etc.) to develop the system.

  • Did you ever try FPDF?

  • Hard to download? Tried here? And I believe in any version of the library,$arquivo = "Profissionais01.php"; is wrong. Pass a pdf to the file name.

  • @lazyFox did not test FPDF, it works the same as mPDF? what is the difference?

  • @Shutupmagda to download from github after downloading the zip as I do? "$file = "Profissionais01.php"; is wrong. Pass a pdf to the file name." so ? I put which name in the file?

  • @Shutupmagda is right his output should be a file pdf and not php. Come up with a name. @Mayke Alission will be different in each of the libraries because they should have different implementations, but will give everything to the same, just read the documentation.

  • Downloading the package by GitHub u unpack and click on the application, as you did. In its place, I would install with the Composer, in accordance with documentation recommends. Installing with the Composer is safer as it installs all library dependencies. The correct thing is to declare an extension .pdf. Thus: Profissionais01.pdf.

  • @Shutupmagda this system that I am creating and my TCC that will be delivered next week, to use Composer I will have to change something in the project? or had changed nothing? after I have no time to reconfigure things.

  • Why not use the DOMPDF library? Tutorial for how to use HERE

Show 3 more comments

1 answer

-1

Try using Dompdf which is simpler and works with php 7+;

Linkd to the Dompdf

This is a simple example of creating PDF.

// reference the Dompdf namespace
use Dompdf\Dompdf;

// instantiate and use the dompdf class
$dompdf = new Dompdf();
$dompdf->loadHtml('hello world');

// (Optional) Setup the paper size and orientation
$dompdf->setPaper('A4', 'landscape');

// Render the HTML as PDF
$dompdf->render();

// Output the generated PDF to Browser
$dompdf->stream();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.