How to display PDF file on yii2 - pdf

I have a audit table and audit_report field.Field type is text .I saved pdf files into folder and saved name to database.I tried to display the pdf on view page. but the box with image sign only getting. I could display jpeg and png files nicely.How to display PDF on view page of yii2 framework.

This would work,
return Yii::$app->response->sendFile($completePath, $filename, ['inline'=>true]);
Input the function with third param as array of value 'inline'=>true to open the file within the browser window.
See the documentation here sendFile()

You could add a button that opens the file in a new tab, but make it link to an action in your controller that returns the file instead of the direct path to the file:
In your view:
<?= Html::a('PDF', [
'controller/pdf',
'id' => $model->id,
], [
'class' => 'btn btn-primary',
'target' => '_blank',
]); ?>
In your controller:
public function actionPdf($id) {
$model = ModelClass::findOne($id);
// This will need to be the path relative to the root of your app.
$filePath = '/your/file/path';
// Might need to change '#app' for another alias
$completePath = Yii::getAlias('#app'.$filePath.'/'.$model->fileName);
return Yii::$app->response->sendFile($completePath, $model->fileName);
}
Aliases - Key Concepts - The Definitive Guide to Yii 2.0

Although the question has been answered there is one thing that needs to be addressed if you have the file content/stream instead of the file path, like for instance you are using Dropbox API and you receive a stream from the API and want to display that file instead of forcing the browser to download.
For this case you can use the sendContentAsFile when attempting to display the PDF file in the browser you will need to specify the mimeType option too along with the "inline"=>true because the default mimeType value is set to application/octet-stream which is used to download a file and to display inline in browser you need to change it to application/pdf.
return Yii::$app->response->sendContentAsFile(
$fileContent,
$filename,
['inline' => true, 'mimeType' => 'application/pdf']
);

Related

How can I add Font Awesome support to Ckeditor5?

I am trying to add Font Awesome support to Ckeditor5-inline and it just removes the "i" tags from HTML when I go in Edit mode.
First download font awesome if you haven't already then
1. Extract the downloaded file (fontawesome.zip) Copy the "fontawesome"
2. folder to "ckeditor/plugins/" folder Open the file
3. "ckeditor/config.js"
configure that like this and clear your browser's cache
config.extraPlugins = 'fontawesome';
config.contentsCss = 'path/to/your/font-awesome.css';
config.allowedContent = true;
In your HTML's section add this code:
<script>CKEDITOR.dtd.$removeEmpty['span'] = false;</script>
after that you can Use toolbargroupname: "FontAwesome" in your toolbar like this
config.toolbar = [
{ name: 'insert', items: [ 'FontAwesome', 'Source' ] }
];
as you commented your are using Django Integration in Django CMS
Django CMS enables adding text-based content to a site using CKEditor which is integrated through the module called djangocms_text_ckeditor. In that module is a static folder and settings.py file, which are setup in a manner that enables fully customizing CKEditor.
you can check here for Django Integration

Show Print and Save Image option using ActionSheetIOS.showShareActionSheetWithOptions

I am trying to implement ActionSheetIOS.showShareActionSheetWithOptions, I am passing the correct url which might be a link to an image or a file(pdf/msword).
I want to provide an option to Print the file or Save Image file when the user clicks on the Share button. But I only see the following options
"Add to reading list", "Copy" and more
Here's my code:
ActionSheetIOS.showShareActionSheetWithOptions({
url: route.passProps.url,
},
(error) => alert(error),
(success, method) => { }
);
I have verified that the url is correct.
When visiting a url, Safari does provide you an option to Print/Save, so I know its possible, but I don't know how.

View PDF in Yii

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.
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

How can i hide the upload list in EAjaxUpload widget

I'm using EAjaxUpload extension to upload multiple files but I also want to use it to upload single ones.
How can I hide the upload list in this case?
Is there any way to configure that in the widget options?
Under config array, you can implement these function:
'onSubmit' => "js:function(){
$('#uploadFile .qq-upload-list').html(''); // This will empty list
}",
'onComplete' => "js:function(id, fileName, responseJSON){
}",
'onProgress' => "js:function(id, fileName, loaded, total){
}",
'onCancel' => "js:function(){
}"

Using Elfinder extension in Yii

I need to use a file manager in pop up which comes on a button click. I am using Yii extension elfinder. I am finding it hard to understand the way of using it. I downloaded the code from bitbucket, put it inside my application in the folder extension. I try to test it using new controller, named it elfcontroller and put the following code (got from the website)
class ElfinderController extends CController
{
public function actions()
{
return array(
'connector' => array(
'class' => 'ext.elFinder.ElFinderConnectorAction',
'settings' => array(
'root' => Yii::getPathOfAlias('webroot') . '/uploads/',
'URL' => Yii::app()->baseUrl . '/uploads/',
'rootAlias' => 'Home',
'mimeDetect' => 'none'
)
),
);
}
}
and i created one more function for rendering the index page(i want the file manager to be in this page)
in the view i wrote the following code
$model = new xxxmodel();
$this->widget('ext.elFinder.ElFinderWidget', array(
'model' => $model,
'attribute' => 'serverFile',
'connectorRoute' => 'admin/elfinder/connector',
)
);
and i included a div for containing it
But i am getting the following error
Alias "ext.elFinder.ElFinderWidget" is invalid. Make sure it points to an existing PHP file and the file is readable.
i tried to include alias in config/main.php
I know i am messing some where with the folder structure
here is the path i am using the extension
C:\xampp\htdocs\project\protected\extensions\ext.elfinder
I returned empty after google searches, can any one please explain me how to use this extension to place the code exactly where it is needed to be?
In general the extensions folder already has the ext alias, so you don't need to set an alias for it.
Then the extension itself should be placed in the extensions folder, somewhat like : project/extensions/extension-name/ . In your case it should be: project\extensions\elFinder , and keep the rest of your code same, i.e continue referring to extension like:
ext.elFinder.ElFinderWidget