Question

I have PDF stored in a folder, now i want to view it via link on my form page. How will i do it using Yii framework.

echo CHtml::link(
        'pdf',
        Yii::app()->createUrl('/uploads/Tutorial.pdf') ,
        array('class'=>'button','target'=>'_blank'));

The error i am receiving mentioned below, i have also tried by including uploads in allow of controller. Error
Error 404
Unable to resolve the request "uploads/Tutorial.pdf".


I am using mPDF to generate PDF but i don't know how to view an already generated PDF using Yii.

No correct solution

OTHER TIPS

You need several more things to make this work. You may have already done some of this and not pasted it but here goes...

I'm assuming you've set up your site to use clean URLs with .htaccess by following this tutorial. Now you can add:

in config/main.php, urlMananger section you need a route to a controller class and action method. This tells Yii which controller to use when you go to the URL /uploads/Tutorial.pdf

'urlManager'=>array(
    'urlFormat'=>'path',
    'showScriptName'=>false,
    'rules'=>array(

         'uploads/<filename:[a-zA-Z]+\.pdf>' => 'upload/viewPdf', // actionViewPdf Method in UploadController class

         // ... all your other rules
    ),
), 

And a controller class, and a method with the same name as in your route above (prepended with action):

 class UploadController extends Controller
 {
      // Put the usual Yii access control stuff here or whatever...

      public function actionViewPdf()
      {
          $filename = $_GET['filename'] . '.pdf';
          $filepath = '/path/to/your/pdfs/' . $filename;

          if(file_exists($filepath))
          {
              // Set up PDF headers
              header('Content-type: application/pdf');
              header('Content-Disposition: inline; filename="' . $filename . '"');
              header('Content-Transfer-Encoding: binary');
              header('Content-Length: ' . filesize($filepath));
              header('Accept-Ranges: bytes');

              // Render the file
              readfile($filepath);
          }
          else
          {
             // PDF doesn't exist so throw an error or something
          }
      }
 }

Then you should be able to use

echo CHtml::link(
    'pdf',
    Yii::app()->createUrl('/uploads/viewPdf', array('filename' => 'Tutorial')) ,
    array('class'=>'button','target'=>'_blank'));

Hope that's helpful.

Well.... are you sure that the uploads folder is available on the server and it is in a location that is link friendly? Yii does not pass everything through it's index.php file, if a file already exists then it just used it like it is. That is why you can access the files in your images or css folder without having a controller for it.

If you are using the normal Yii structure then you should have your "uploads" folder next to your protected folder.

If your uploads folder is not in a web accessible location, use what JamesG told you to. The only change I would to is to the pattern, it only matches letters, you might have other chars in the name too.

Do not add ".pdf" to your link:

Yii::app()->createUrl('/uploads/Tutorial')

in PHP you can print your pdf like this (must be in the actionTutorial):

$mPDF = Yii::app()->ePdf->mpdf();
$mPDF->WriteHTML($this->render('tutorial', array(), true));
$mPDF->Output();

The tutorial is a simply html page that should be rendered into your pdf.

Here you can find the doku to the mpdf Yii extension

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top