Yii Framework Forum: Yii: Generating Pdf Document From Server Response By Streaming Html Into Pdf - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Yii: Generating Pdf Document From Server Response By Streaming Html Into Pdf Rate Topic: -----

#1 User is offline   FaisalKhan 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 34
  • Joined: 17-September 12

Posted 04 November 2012 - 05:45 AM

I am new to web development and remember from past that a friend of mine once had an implementation in which Server response stream of HTML was stored in a variable and then output as PDF, this was JSP some ages ago. I don't know how to achieve this in Yii rather bit skeptical if I even remember it right. I have explored TCPDF and can produce the PDF by coding the HTML tags, however, I am looking into the option to stream the output of a URL response in a variable and then use that to generate the PDF. I did the following but it is not working. I receive error at filesize($filename):
$pdf->AddPage();
$filename = 'http://localhost/webapp/index.php/link/to/some/page';
$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename));
fclose($handle);
$pdf->writeHTML($contents ,true);


I am bit lost on how to achieve this, all the help is much appreciated. Thanks in advance.
0

#2 User is offline   guaruja 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 68
  • Joined: 10-March 12

Posted 04 November 2012 - 06:45 AM

View PostFaisalKhan, on 04 November 2012 - 05:45 AM, said:

I am new to web development and remember from past that a friend of mine once had an implementation in which Server response stream of HTML was stored in a variable and then output as PDF, this was JSP some ages ago. I don't know how to achieve this in Yii rather bit skeptical if I even remember it right. I have explored TCPDF and can produce the PDF by coding the HTML tags, however, I am looking into the option to stream the output of a URL response in a variable and then use that to generate the PDF. I did the following but it is not working. I receive error at filesize($filename):
$pdf->AddPage();
$filename = 'http://localhost/webapp/index.php/link/to/some/page';
$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename));
fclose($handle);
$pdf->writeHTML($contents ,true);

I am bit lost on how to achieve this, all the help is much appreciated. Thanks in advance.


Try using Curl. If you are not familiar with it, there is also a yii extension that may facilitate things for you CURL for Yii
0

#3 User is offline   bennouna 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,166
  • Joined: 05-January 12
  • Location:Morocco

Posted 04 November 2012 - 07:01 AM

I'm not directly answering your post question, but is the page by any chance Yii-controlled? I mean, is it a controller that generates it?

In that case, you should find easier ways. I know that pdf extension has a WriteHTML() method, which you can pass a view rendered by Yii, so maybe TCPDF does the same?
0

#4 User is offline   FaisalKhan 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 34
  • Joined: 17-September 12

Posted 04 November 2012 - 09:06 AM

View Postbennouna, on 04 November 2012 - 07:01 AM, said:

I'm not directly answering your post question, but is the page by any chance Yii-controlled? I mean, is it a controller that generates it?

In that case, you should find easier ways. I know that pdf extension has a WriteHTML() method, which you can pass a view rendered by Yii, so maybe TCPDF does the same?



Yes, I am planning to make it Yii based. I am thinking of creating an action in controller which would be called from a button, the action would take the HTML of an other view(HTML input streaming comes here) and pass it to the TCPDF->pdf object and I will save the PDF on the server. Once the PDF is successfully generated and saved, I will open a view to user which would let him see that PDF. User can then save it to local machine or close it. This is all how I am planning to implement, please correct me if I am wrong all the way and suggest an approach which would do the same.
I am on to what you suggested, it seems promising, will get back with my results.
Thanks a lot.
0

#5 User is offline   bennouna 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,166
  • Joined: 05-January 12
  • Location:Morocco

Posted 04 November 2012 - 12:33 PM

It's exactly what I meant FaisalKhan. That's something I coded several times using the extension I mentioned above. Works as advertised :)

Edit: so Instead of passing the page url and reading its contents, you can do
$pdf->writeHtml($this->render('view'))

0

#6 User is offline   FaisalKhan 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 34
  • Joined: 17-September 12

Posted 05 November 2012 - 04:38 AM

View Postbennouna, on 04 November 2012 - 12:33 PM, said:

It's exactly what I meant FaisalKhan. That's something I coded several times using the extension I mentioned above. Works as advertised :)

Edit: so Instead of passing the page url and reading its contents, you can do
$pdf->writeHtml($this->render('view'))



It worked exactly the way I wanted. So many thanks to you! Cheers :)
0

#7 User is offline   FaisalKhan 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 34
  • Joined: 17-September 12

Posted 06 November 2012 - 11:20 AM

View Postbennouna, on 04 November 2012 - 12:33 PM, said:

It's exactly what I meant FaisalKhan. That's something I coded several times using the extension I mentioned above. Works as advertised :)

Edit: so Instead of passing the page url and reading its contents, you can do
$pdf->writeHtml($this->render('view'))



Need a little push on one thing. I have generated the PDF and can open it with 100% results but i am unable to present the generated PDF to user after it has been created. I am trying to open it soon after it created and tried the following in my controller action but in vain:

$html2pdf->Output($pdfFilename,EYiiPdf::OUTPUT_TO_FILE);//This is where I get the PDF successfully.
$filecontent=file_get_contents($pdfFilename);
header("Content-Type: text/plain");
header("Content-disposition: attachment; filename=$pdfFilename");
header("Pragma: no-cache");
echo $filecontent;
exit;

0

#8 User is offline   FaisalKhan 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 34
  • Joined: 17-September 12

Posted 06 November 2012 - 11:37 AM

View PostFaisalKhan, on 06 November 2012 - 11:20 AM, said:

Need a little push on one thing. I have generated the PDF and can open it with 100% results but i am unable to present the generated PDF to user after it has been created. I am trying to open it soon after it created and tried the following in my controller action but in vain:

$html2pdf->Output($pdfFilename,EYiiPdf::OUTPUT_TO_FILE);//This is where I get the PDF successfully.
$filecontent=file_get_contents($pdfFilename);
header("Content-Type: text/plain");
header("Content-disposition: attachment; filename=$pdfFilename");
header("Pragma: no-cache");
echo $filecontent;
exit;



Oh never mind.....! a small mistake and now it working. I was rendering the view along with PDF generation because of which the Open/Save Dialog box was not coming. After I commented out the view code, it worked fine. Thanks in advance.
Cheers :)
0

#9 User is offline   bennouna 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,166
  • Joined: 05-January 12
  • Location:Morocco

Posted 06 November 2012 - 11:38 AM

Well I guess this should work:

$html2pdf->Output($pdfFilename, EYiiPdf::OUTPUT_TO_FILE);
header('Content-Type: application/pdf');
header('Cache-Control: must-revalidate'); // Depends on your project
$offset = 3600*24*30 ; // 1 month, adapt to your needs
$ExpStr = 'Expires: ' . gmdate('D, d M Y H:i:s', time() + $offset) . ' GMT';
header($ExpStr);
readfile($pdfFilename);

0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users