Welcome Guest, Not a member yet? Register   Sign In
Generating a PHP and HTML page to PDF using mPDF
#1

[eluser]haris244808[/eluser]
I have transfered all mPDF directory to Codeigniter helpers directory
and
i have created a helper page (pdfexport_helper.php)
Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');


      if (!function_exists('create_pdf')) {

          function create_pdf($data, $filename = "") {
            
              require 'MPDF/mpdf.php';
              $mypdf = new mPDF();
              $mypdf->WriteHTML($data);
              $mypdf->Output($file_name . 'pdf', 'I');
          }

}

i created a controller (pdf.php) where i am calling the page that should be generated to pdf
Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Pdf extends CI_Controller{

function __construct(){
  
  parent::__construct();
}

public function pdf_report($user_id){

  $this->load->helper(array('pdfexport'));
  $data = file_get_contents(base_url('profile/show_profile/'.$user_id)); // here is the page that i call, the page information changes according to id (user_id)...simply its a profile page that changes according to which user is logged in
  $filename = 'profile'.$user_id.'_'.date('dMY');
  create_pdf($data, $filename);
  }


}

but it doesnt generate the page i want, instead it generates the very first page (login page).

what am i missing here??
#2

[eluser]TheFuzzy0ne[/eluser]
First of all, you shouldn't need to make an HTTP request to get the profile information. You should be able to do that by querying your model, and using a view (the code you need should already be written in the show_profile() method of your Profile controller. By making that HTTP request, you are essentially doubling your server's load, because CodeIgniter is loaded twice in a single request.

Your problem is caused by the fact your HTTP request doesn't send any cookies, and therefore is not logged into your Web site.

Hope this helps.
#3

[eluser]haris244808[/eluser]
[quote author="TheFuzzy0ne" date="1369162028"]First of all, you shouldn't need to make an HTTP request to get the profile information. You should be able to do that by querying your model, and using a view (the code you need should already be written in the show_profile() method of your Profile controller. By making that HTTP request, you are essentially doubling your server's load, because CodeIgniter is loaded twice in a single request.

Your problem is caused by the fact your HTTP request doesn't send any cookies, and therefore is not logged into your Web site.

Hope this helps.[/quote]

Thnx for the fast reply.
Can you make ur answer a little bit clear pls.

here is a show_profile function:
Code:
function show_profile($user_id){

  $data['page_title'] = 'User Profile';
  $data['content'] = 'profile_view';

  $user_results = $this->users_model->select_user_by_id($this->session->userdata('id'));

  $data['user_results'] = $this->users_model->select_user_by_id($user_id);
   if($data['user_results'] == FALSE){echo "Bad Request"; die();}

  $this->load->view('includes/template', $data);
}
#4

[eluser]TheFuzzy0ne[/eluser]
Instead of:
Code:
public function pdf_report($user_id){
    $this->load->helper(array('pdfexport'));
    $data = file_get_contents(base_url('profile/show_profile/'.$user_id)); // here is the page that i call, the page information changes according to id (user_id)...simply its a profile page that changes according to which user is logged in
    $filename = 'profile'.$user_id.'_'.date('dMY');
    create_pdf($data, $filename);
}

Do something like:
Code:
public function pdf_report($user_id){
    $this->load->helper('pdfexport');

    $data['page_title'] = 'User Profile';
    $data['content'] = 'profile_view';
$data['user_results'] = $this->users_model->select_user_by_id($this->session->userdata('id'));

    $filename = 'profile'.$user_id.'_'.date('dMY');
    create_pdf($this->load->view('includes/template', $data, TRUE), $filename);
}

Now you aren't making an extra HTTP request to get the profile markup (as you were with file_get_contents()Wink. Instead you're just loading a view and passing the output from that into create_pdf().
#5

[eluser]haris244808[/eluser]
[quote author="TheFuzzy0ne" date="1369165564"]Instead of:
Code:
public function pdf_report($user_id){
    $this->load->helper(array('pdfexport'));
    $data = file_get_contents(base_url('profile/show_profile/'.$user_id)); // here is the page that i call, the page information changes according to id (user_id)...simply its a profile page that changes according to which user is logged in
    $filename = 'profile'.$user_id.'_'.date('dMY');
    create_pdf($data, $filename);
}

Do something like:
Code:
public function pdf_report($user_id){
    $this->load->helper('pdfexport');

    $data['page_title'] = 'User Profile';
    $data['content'] = 'profile_view';
$data['user_results'] = $this->users_model->select_user_by_id($this->session->userdata('id'));

    $filename = 'profile'.$user_id.'_'.date('dMY');
    create_pdf($this->load->view('includes/template', $data, TRUE), $filename);
}

Now you aren't making an extra HTTP request to get the profile markup (as you were with file_get_contents()). Instead you're just loading a view and passing the output from that into create_pdf().[/quote]

Yes i understood the logic and thank you very much for that.
but i need to generate pdf when a href is clicked..
show_profile function is for loading the profile. With what u did, means that when the profile is loaded a pdf also will be generated...

The show profile function sends to the profile where a href link for generating the pdf is included..

which will be the best solution in this case?
#6

[eluser]haris244808[/eluser]
I did smth like this...however it prits me a blank page

Code:
public function pdf_report($user_id){
  
  $data['content'] = 'profile_view';

  $user_results = $this->users_model->select_user_by_id($this->session->userdata('id'));

  $data['user_results'] = $this->users_model->select_user_by_id($user_id);

  $this->load->helper(array('pdfexport'));
  $filename = 'profile'.$user_id.'_'.date('dMY');
    
  $html = $this->load->view('includes/template', $data);
//here i am planning to make if isset the link (if the link is clikced)...i didnt do just for testing purpose
  create_pdf($html, $filename);
}
#7

[eluser]TheFuzzy0ne[/eluser]
No, what I did does NOT generate a PDF everytime the profile is viewed. It will generate a PDF when you go to http://yoursite.com/pdf/pdf_report/<user_id>, which is exactly what you're trying to do, is it not?

You need to pass TRUE as the third parameter to $this->load->view() in order to return the contents. What you're doing will just send the view to the browser.
#8

[eluser]haris244808[/eluser]
[quote author="TheFuzzy0ne" date="1369217732"]No, what I did does NOT generate a PDF everytime the profile is viewed. It will generate a PDF when you go to http://yoursite.com/pdf/pdf_report/<user_id>, which is exactly what you're trying to do, is it not?

You need to pass TRUE as the third parameter to $this->load->view() in order to return the contents. What you're doing will just send the view to the browser.[/quote]

Ok thnx for that..
and what if i want to create a costum PDF page...to generate one with info from db and costumize the html + css ? From where i should start? which will be the best method??

because when i generate the page it has some floating problems where it doesn not generate the page correctly as it is?
#9

[eluser]haris244808[/eluser]
OK
i started using FPDF and it works fine...however i have problem now with HTML tags...

when i display conetent from MYSQL db (which holds content from Text Editor together with HTML tags)... id generates the PDF showing contetn together with HTML tags...

How can i hide HTML tags when generating PDF.

i tried strip_tags...htmlspecialchars()....dont work

any suggesstion???
#10

[eluser]TheFuzzy0ne[/eluser]
Whilst mPDF has been designed to specifically parse HTML and make it into a PDF, it doesn't support all HTML tags and CSS attributes. You need to design your HTML so it's as simple as possible, and try to avoid floating elements wherever possible. Instead, you can use absolute positioning, which should work much better. Also, your CSS should be inline CSS, meaning you should use the style attribute.

Correct:
Code:
&lt;body&gt;
    &lt;div style="border:1px solid #ff0000;">
        Lorem ipsum dolor sit amet, consectetur adipiscing elit...
    </div>
    &lt;!-- ... ->
&lt;/body&gt;

Wrong:
Code:
&lt;head&gt;
    &lt;style type="text/css"&gt;
        .myclass {
            border: 1px solid #ff0000;
        }
    &lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;
    <div class="myclass">
        Lorem ipsum dolor sit amet, consectetur adipiscing elit...
    </div>
    &lt;!-- ... ->
&lt;/body&gt;

Using the code I posted, you should be able to apply that to get the results you want. It works just like loading a view in the sense that you pass the variables into the view, but the difference being that instead of outputting the view, you're passing the output into your create_pdf() function.

Hope this helps.




Theme © iAndrew 2016 - Forum software by © MyBB