Change logo in TCPDF via form

Asked

Viewed 307 times

2

I created a PDF generator with the TCPDF, It’s working okay, I’m just having a little problem. I want the logo-marca to enter, whatever I choose in the form.

Follows the codes.

Code of the form sending the data (index.php)

<table align="right" width="100%">
    <tr> 
        <td size="50px">Logo
            <div style="padding-top:5px;">
                <input name="logo" type="radio" 
                       value="http://www.example.com/logo1.jpg" /> LOGO 1
                <input name="logo" type="radio" 
                       value="http://www.example.com/logo2.jpg" /> LOGO 2
                <input name="logo" type="radio" 
                       value="http://www.example.com/logo3.jpg" /> LOGO 3
            </div>
        </td>
    </tr>
</table>

Code php of the file that generates the pdf (invoice2.php)

<?
require_once('tcpdf_include.php');
class MYPDF extends TCPDF {
    public function Header() {
        $image_file = 'http://www.example.com/logo.jpg';
      //$pdf->Image('images/image_demo.jpg', $x, $y, $w, $h, 'JPG', '',
      //     '', false, 300, '', false, false, 0, $fitbox, false, false);
        $this->Image($image_file,'',8,80,'', 'JPG', '', 'T', false, 300,
                     'L', false, false, 0, false, false, false);
    }
    // Page footer
    public function Footer() {
        // Position at 15 mm from bottom
        $this->SetY(-15);
        // Set font
        $this->SetFont('helvetica', 'I', 8);
    }
}

I want you to take for example: The person who selects the LOGO 1, by generating the PDF, the image selected appears at the top of the PDF generated. At the moment, the image that appears is the one in the code of invoice2.php.

public function Header() {
    $image_file = 'http://www.example.com/logo.jpg';
}

Could someone help me with this?

1 answer

1

You can do this using the methods GET or POST when submitting the form. (In the example below I used the POST)

The first thing I did to illustrate was to change the code HTML, adding the form, see:

<form method="post" action="seu_script_que_gera_o_pdf.php" enctype="multipart/form-data">
    <table align="right" width="100%">
        <tr>
            <td size="50px">Logo
                <div style="padding-top:5px;">
                    <input name="logo" type="radio" 
                           value="http://www.example.com/logo1.jpg" /> LOGO 1
                    <input name="logo" type="radio" 
                           value="http://www.example.com/logo2.jpg" /> LOGO 2
                    <input name="logo" type="radio" 
                           value="http://www.example.com/logo3.jpg" /> LOGO 3
                </div>
            </td>
        </tr>
    </table>
</form>

Now we can change the script that generates the PDF to behave accordingly see:

<?
require_once('tcpdf_include.php');

class MYPDF extends TCPDF {
    public function Header($logo) {
        $image_file = $logo; //'http://www.example.com/logo.jpg';
      //$pdf->Image('images/image_demo.jpg', $x, $y, $w, $h, 'JPG', '', 
                    '', false, 300, '', false, false, 0, $fitbox, false, false);
        $this->Image($image_file,'',8,80,'', 'JPG', '', 'T', false, 300, 
                     'L', false, false, 0, false, false, false);
    }
    // Page footer
    public function Footer() {
        // Position at 15 mm from bottom
        $this->SetY(-15);
        // Set font
        $this->SetFont('helvetica', 'I', 8);
    }
}

if ($_SERVER['REQUEST_METHOD'] == 'POST'){
    $image = $_POST['logo'];
  
    $myPdf = new MYPDF();
    $myPdf->Header($logo);
}

The method Header has been changed so that it is possible to indicate the desired image when the form is submitted.

  • 1º: Strict Standards: Declaration of MYPDF::Header() should be compatible with TCPDF::Header() in /invoice2.php on line 12 2º: Warning: Missing argument 1 for MYPDF::Header(), called in /home/evolucao/public_html/celg2/tcpdf.php on line 3537 and defined in /invoice2.php on line 13 3rd: TCPDF ERROR: [Image] Unable to get the size of the image:

  • you can choose to change the constructor method to remove the Strict standards. See the code I changed in the Pastebin http://pastebin.com/r16EPGMV

Browser other questions tagged

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