-2

I am trying to use mpdf to download a php invoice generated from a database. Unfortunately it only directs to the page and doesn't download. So is it poosible for mpdf to download php files from another directory and how can I achieve that.

<?php
require_once __DIR__ . '/vendor/autoload.php';

$mpdf = new \Mpdf\Mpdf();
include '../invoice-view.php';
$data = 'invoice-view.php';

$mpdf->WriteHTML($data);
$mpdf->Output('invoice.pdf', 'D');
?>

It redirects to the page and doesn't download the php file.

M. Eriksson
  • 13,450
  • 4
  • 29
  • 40
  • You need to set correct headers at the beginning of your file to start the downloading process: https://stackoverflow.com/a/20080402/2392957 – alexey-novikov Jul 25 '19 at 06:00
  • Doesn't the method [WriteHTML()](https://mpdf.github.io/reference/mpdf-functions/writehtml.html) take a string _containing_ the HTML as the first argument, not a file path? – M. Eriksson Jul 25 '19 at 06:00
  • @alexey its working when downloading files on its own directory but not downloading from other files – Ibrahim Claude Jul 25 '19 at 06:11
  • @MagnusEriksson Please clarify – Ibrahim Claude Jul 25 '19 at 06:11
  • The first argument should be a string containing the html document you want to print, something like: `"..."`. You're not doing that. You're passing in a string which contains `"invoice-view.php"`. As far as mpdf is concerned, that's the string you want to print. – M. Eriksson Jul 25 '19 at 06:29
  • _“its working when downloading files on its own directory but not downloading from other files”_ - please properly explain what that actually means. Give examples of where which files are located in that scenario. – misorude Jul 25 '19 at 08:03
  • @misorude my question is that i have an invoice system that generates invoice. I want to download it but doesn't seem to work using mpdf. I want to download the same exact invoice that the user is seeing. How will i get this to work? – Ibrahim Claude Jul 26 '19 at 05:49
  • @misorude I tried mpdf to fecth data from invoice-view.php but its printing evering in the page i.e $row['name']; ?> tags instead of displaying the form's name. – Ibrahim Claude Jul 26 '19 at 05:51

2 Answers2

1

Solution

 <?php
    require_once __DIR__ . '/vendor/autoload.php';

    $mpdf = new \Mpdf\Mpdf();       
    $data = file_get_contents('invoice-view.php');

    $mpdf->WriteHTML($data);
    $mpdf->Output('invoice.pdf', 'D');
    ?>

Or you get content using CURL function.

<?php
$url="DOMAIN_NAME"."/invoice-view.php";

$timeout = 5;
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_NOBODY, 0);
    curl_setopt ( $ch , CURLOPT_RETURNTRANSFER , 1 );
    curl_setopt ( $ch , CURLOPT_CONNECTTIMEOUT , $timeout );
    $data = curl_exec($ch);
    curl_close($ch);


$mpdf->WriteHTML($data);
        $mpdf->Output('invoice.pdf', 'D');
?>

using curl Function example

test.php

$params=array('name'=>'XYZ');

$data = file_get_contents_by_curl('http://localhost/test2.php',$params);

$mpdf->WriteHTML($data);
$mpdf->Output('invoice.pdf', 'D');

function file_get_contents_by_curl($url,$params) {
    $timeout = 5;
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_NOBODY, 0);
    curl_setopt ( $ch, CURLOPT_POSTFIELDS, $params );   
    curl_setopt ( $ch, CURLOPT_CUSTOMREQUEST, 'POST' ); 
    curl_setopt ( $ch , CURLOPT_RETURNTRANSFER , 1 );
    curl_setopt ( $ch , CURLOPT_CONNECTTIMEOUT , $timeout );
    $data = curl_exec($ch);
    curl_close($ch);
    return $data;
}

test2.php

<h1>
Hi,<?php echo $_POST['name'];?></h1>
Jimit H.
  • 185
  • 1
  • 11
0

For initilalizing $data you should use this

$data = require '../invoice-view.php';

In invoice-view.php generate html string and return it.

potiev
  • 546
  • 2
  • 11