title_for_layout while viewing pdf with cakeResponse - cakephp

i'm viewing pdf files from database using this function:
public function pdfPreview($taskId){
$file = $this->StoredFile->find('first', array(
'conditions' => array(
'StoredFile.id' => $taskId
),
'contain' => false
));
$this->response->body($file['StoredFile']['data']);
$this->response->type('pdf');
//$this->response->header('Content-Disposition', 'inline');
//Return response object to prevent controller from trying to render a view
return $this->response;
}
Is there any way to set title for layout with CakeResponse while not rendering layout, because i'm trying to set filename as title, but i didn't found solution?

Usually the $title_for_layout view var is passed to the html and put between <title> tags.
However, as I understand, you are outputting a PDF using raw data from de database. This is then interpreted by the browser to render a PDF view. The browser thinks it is displaying a file, so when you set the filename in the Content-Disposition header it will display this in its title. Like they do on https://stackoverflow.com/a/2016016/1535656

Related

Attach a file with contentId from inside an email Layout

What I have
In my Controller (CakePHP 3.8):
$email = new \Cake\Mailer\Email();
$email
->emailFormat('html')
->setLayout('mylayout')
->setTo($to)
->setSubject($subject)
->addAttachments([
'logo.png' => [
'file' => $logoImage,
'mimetype' => mime_content_type($logoImage),
'contentId' => 'mylogo'
]
])
->send($message);
And then in src/Template/Layout/Email/html/mylayout.ctp:
<img src="cid:mylogo">
That works.
What I want
I want to embed that image from within the Layout file.
So I don't have to repeat ->addAttachments() every time I'm sending an e-mail
If I replace the image I can do it in the Layout only once
If I use another Layout that doesn't need the image, it won't be attached and sent
Using contentId looks like an option. But how do I do it from inside the Layout?
What I tried
Sending the image as data/uri, but that didn't work
Searching for some Cake\Mailer instance under $this when in Layout, but couldn't find any

CakePHP Clicking PDF Link and View it on New Tab

Ok, so I have a web app that uploads a file to the webserver. My input fields in my upload form include: type of upload (dropdown list), title, description, and the file to be uploaded, which is a PDF.
Once the PDF file is uploaded, the download link will appear in another page for the public to see. In addition, the title typed in the input field is the download link.
Now, I want to change my code. Instead of downloading it directly when the link is clicked I want it to open in a new tab, so the users can first look at the PDF file then download it from there.
Here are my codes.
Controller:
public function sendFile(){
$id = $this->request->params['pass'][0];
$staffup = $this->StaffUpload->find('first', array('conditions' => array('iduploads'=>$id)));
$this->response->file($staffup['StaffUpload']['dest'], array('download' => true, 'name' => $staffup['StaffUpload']['title']));
return $this->response;
}
The code above is the download function.
public function resources() {
$this->layout = 'website';
$this->set('staff_uploads', $this->StaffUpload->find('all', array('conditions' => array('type' => 'Resource'))));
}
The code above is the view wherein I show all uploaded files which type is Resources.
View:
<?php
foreach ($staff_uploads as $staff_uploads) {
?>
ul>
<li>
<?php
echo $this->Html->link($staff_uploads['StaffUpload']['title'], array('controller' => 'websites', 'action' => 'sendFile', $staff_uploads['StaffUpload']['iduploads']));
?>
</li>
</ul>
<?php
}
?>
The code above shows the view.
So yeah, back to the question. I want to change the download link to a link in which when clicked, will show the PDF file in a new tab. How do I do that? And by the way, the codes posted above are all working properly. I just want to change my code so that it will be viewed in a new tab when clicked.
Thank you!
According to the docs:
echo $this->Html->link(
'Enter',
'/pages/home',
array('target' => '_blank')
);

how to rander .html file using cakephp : CakePHP

I am working on CakePHP and I have a URL http://admin.example.com/Pages .
How can I create http://admin.example.com/Pages.html ? Is there any solution or component to solve this issue?
According to CakeBook , You can define extensions you want to parse in routes
E.g. Write the code below in app/Config/routes.php
Router::parseExtensions('html');
This will allow you to send .html extenstion in routes(url)
Not create a link
E.g:
$this->Html->link('Link title', array(
'controller' => 'pages',
'action' => 'index',
'ext' => 'html'
));
When you click that link you will get url in browser something like this
http://admin.example.com/pages/index.html
Do you really need this? CakePHP is automatically render view files from View folder
You can create (if its PagesController and index methond) index.ctp in app/View/Pages folder and it will be automatically render.
You can use file_get_contents function to read files. Something like this:
// in controller
function index() {
$content = file_get_contents('path/to/file.html');
echo $content;
die();
}

Drupal 7, default_value for a file field in a custom form

I've created a formular where the user can upload an image using a file field. In another formular the user is able to change the image he previous uploaded.
So far I was able that the user can upload a new picture or let the field empty and the existing image remains. My question is, how do I show the user in a nice way which file he previously uploaded with the field? Setting a #default_value (#code below) does nothing for me.
//$smallimage = fid of image
$form['smallimage'] = array(
'#type' => 'file',
'#title' => t('Image'),
'#default_value' => $smallimage,
'#description' => t('Upload a file, allowed extensions: jpg, jpeg, png, gif'),
);
Edit:
Switched back to managed files. I've no types to bind my files/images to so I bind them to the first node and the first user. Not a clean solution but it works.
Has anyone a solution/workaround without switching to managed_file?
Thank you in advance
On Drupal FAPI, '#default_value' is not a valid property for type "file", if you want to use the property #default_value in that case you can use
'#type' => 'managed_file'
and your code should look like this,
$form['file'] = array(
'#title' => t('Upload image'),
'#type' => 'managed_file',
'#description' => t('Images must be one of jpg, bmp, gif or png formats.'),
'#default_value' => $fid, //here you need to provide the file id (get it from database or $file_obj->fid).
'#upload_location' => 'public://'
);

Render view to variable in CakePHP 1.3 (to generate pdf and download file)

I'm trying to render a view to a variable.
This variable will then be used to generate a pdf.
Then that pdf should be downloaded with the Media view.
Here's my controller code:
$dir = ROOT . '/app/tmp/evaluationpdf/';
$path = $dir . $evaluationid . '.pdf';
$evaluation = $this->SelfEvaluation->find('first', array(
'conditions' => array('SelfEvaluation.id' => $evaluationid),
'contain' => array('Submission' => array('Application'), 'Applicant', 'Member')));
$this->set(compact('evaluation'));
$this->output = '';
$this->layout = false;
$html = $this->render('/elements/self_evaluation_pdf');
$this->_generate_pdf($html, $path);
$this->view = 'Media';
$params = array(
'id' => $evaluationid . '.pdf',
'name' => $evaluationid,
'download' => true,
'extension' => 'pdf',
'path' => $dir,
);
$this->set($params);
The file is created as it should, but the first '$this->render' output is also sent to the browser.
The file is never downloaded.
Any idea on how to fix this?
The simple fix is to just set $this->output to '' after your first render() call.
The more correct way is to use requestAction() instead of render().
In CakePHP 2.x I did the following in order to use a view to generate a barcode label format:
$response = $this->render('/Labels/' . $printer['Printer']['model'] . '/manifest', 'ajax');
$body = $response->body();
$response->body('');
Which gave me the view data as $body. Then I could just redirect or if the request was ajax just set autoRender to false and return ''.
It kind of muddies the MVC waters but it is simple.
You just have to write the code below in self_evaluation_pdf.ctp
header("Content-Disposition: attachment; filename='downloaded.pdf'");
header('Content-type: application/pdf');
The dynamic content in this view will be downloaded as a PDF file on the client side.

Resources