How to send blade as attachment in pdf format in laravel? - laravel

I want to send blade file as attachment in mail with laravel. I am
writing below code for this but its not working.
Below is my controller where i am getting data from form and then send
this data to another controller where my attachment function is
called.
$data = array('shareholders'=>$request->com_shareholder_count,'contract_send'=>$request->contract_send);
$to = $mail_log->to_email_id = $request->email_id;
$mail = Mail::to($to)->send(new SendMailable($data));
This is my SendMailable controller :
$director_info_pdf = view('directors_info',compact('data'))->render();
On return this variable it shows me error :
message: "Invalid view.", exception: "InvalidArgumentException",…}
exception: "InvalidArgumentException"
file: "C:\xampp\htdocs\caps_admin\vendor\laravel\framework\src\Illuminate\Mail\Mailer.php"
line: 285
message: "Invalid view."
After this line i am writing code to attach my files. where i am sending some files other 3 files are directly send from folder. And last one is attached from blade file.
->attachdata($director_info_pdf, 'dynamic_data.pdf')
->attach( $public_path.'/'.'contract.pdf', [
'as' => 'contract.pdf',
'mime' => 'application/pdf',
])
->attach($public_path.'/'.'HMRC.pdf',[
'as' => 'HMRC.pdf',
'mime' => 'application/pdf',
])
->attach($public_path.'/'.'clientR3.pdf',[
'as' => 'contract1.pdf',
'mime' => 'application/pdf',
]);
I am able to send mail with all 4 files as attachment. But when i am trying to open my files in mail rest 3 files are working as pdf. but ->attachdata($director_info_pdf, 'dynamic_data.pdf') this file get corrupted.
I dont know how to first change this file into pdf and then send as attachment.
I am using snappy for pdf.

I think you want to send pdf as an attachment in your email without saving it in your system .
your can refer the following code to do this .
public function sendmail()
{
$data["email"]='useremail#gmail.com';
$data["client_name"]='user_name';
$data["subject"]='sending pdf as attachment';
//Generating PDF with all the post details
$pdf = PDF::loadView('userdata'); // this view file will be converted to PDF
//Sending Mail to the corresponding user
Mail::send([], $data, function($message)use($data,$pdf) {
$message->to($data["email"], $data["client_name"])
->subject($data["subject"])
->attachData($pdf->output(), 'Your_Post_Detail.pdf', [
'mime' => 'application/pdf',
])
->setBody('Hi, welcome user! this is the body of the mail');
});
}
hope it helped you .
for better understanding you can follow this article
how to send pdf as an attachment in email in laravel

I dont know how to first change this file into pdf and then send as attachment. I am using snappy for pdf.
So that's the problem right there. You are attaching an HTML file, and simply naming it as if it's a PDF. That won't work. You do indeed need to generate this PDF from the HTML view that you render.
Here is the documentation for snappy:
https://github.com/barryvdh/laravel-snappy#usage
I suggest you to read up on it, as the examples given there are pretty straight forward and easy to understand.
This is untested, but if you've set up Snappy correctly, based on your code, something like this should work:
$snappy = App::make('snappy.pdf');
$director_info_pdf = $snappy->getOutputFromHtml(view('directors_info',compact('data'))->render());

Related

How to display PDF Documents on the browser using a View in Laravel 5.8

I'm working on a web application using Laravel 5.8, I'm new to Laravel framework. I would like to display PDF documents on the browser when users click on some buttons. I will allow authenticated users to "View" and "Download" the PDF documents.
I have created a Controller and a Route to allow displaying of the documents. I'm however stuck because I have a lot of documents and I don't know how to use a Laravel VIEW to display and download each document individually.
/* PDFController*/
public function view($id)
{
$file = storage_path('app/pdfs/') . $id . '.pdf';
if (file_exists($file)) {
$headers = [
'Content-Type' => 'application/pdf'
];
return response()->download($file, 'Test File', $headers, 'inline');
} else {
abort(404, 'File not found!');
}
}
}
/The Route/
Route::get('/preview-pdf/{id}', 'PDFController#view');
Mateus' answer does a good job describing how to setup your controller function to return the PDF file. I would do something like this in your /routes/web.php file:
Route::get('/show-pdf/{id}', function($id) {
$file = YourFileModel::find($id);
return response()->file(storage_path($file->path));
})->name('show-pdf');
The other part of your question is how to embed the PDF in your *.blade.php view template. For this, I recommend using PDFObject. This is a dead simple PDF viewer JavaScript package that makes embedding PDFs easy.
If you are using npm, you can run npm install pdfobject -S to install this package. Otherwise, you can serve it from a CDN, or host the script yourself. After including the script, you set it up like this:
HTML:
<div id="pdf-viewer"></div>
JS:
<script>
PDFObject.embed("{{ route('show-pdf', ['id' => 1]) }}", "#pdf-viewer");
</script>
And that's it — super simple! And, in my opinion, it provides a nicer UX for your users than navigating to a page that shows the PDF all by itself. I hope you find this helpful!
UPDATE:
After reading your comments on the other answer, I thought you might find this example particularly useful for what you are trying to do.
According to laravel docs:
The file method may be used to display a file, such as an image or PDF, directly in the user's browser instead of initiating a download.
All you need to do is pass the file path to the method:
return response()->file($pathToFile);
If you need custom headers:
return response()->file($pathToFile, $headers);
Route::get('/show-pdf/{id}', function($id) {
$file = YourFileModel::find($id);
return response()->file(storage_path($file->path));
})->name('show-pdf');
Or if file is in public folder
Route::get('/show-pdf', function($id='') {
return response()->file(public_path().'pathtofile.pdf');
})->name('show-pdf');
then show in page using
<embed src="{{ route('show-pdf') }}" type="text/pdf" >

Image not displayed in postman (readable) using Laravel API

The image is saved to the database and file path specified in the response. Trying to display the image or download it, it is not the displayed. I tried to read the same image but it is working.
storage_path('app/uploads/images/3.png')
I read the 3.png stored in the directory and it is working without displaying error but to display the image it doesn't display image.
[![if (empty($imag)){
return response()->json(\[
'success' => false,
'message' => 'There was a problem creating the postcard!'
\], 201);
}
return response()->json(\[
'success' => true,
'data' => file_get_contents(storage_path('app/public/uploads/images/'.$exportName)),
'message' => 'Postcard created successfully!'
\],200);
}
The directory where the image is saved is storage/app/public/uploads/
I used php artisan storage:link
Your storage path is
App/upload/images/3.png
And you are uploding image in
App/upload/public/images/3.png
So database is storing above path and image is not uploded in above path
Just remove public from above path it works...

Laravel 5.1/5.6 How to send a url in email blade

I am using Mail::send function to send emails to my end users with dynamically generated links in them Below is my sample code for the function -
$myemail = "some email adderess";
$myurl = "some url that will be emailed";
Mail::send('emails.mybladetemplate', ['myemail' => $myemail] , function ($message) use ($myemail){
$message->subject('Your favorite links');
$message->from('someone#company.com', 'Company');
$message->to($myemail);
});
I am having trouble passing $myurl to the blade template and publishing it. I even used HTML{{ }} in my blade template but no success. I have tested Mail::send with other templates and it works fine. Just not when I pass the variables.
Added ['myemail' => $myemail, 'myurl' => $myurl] and use ($myemail, $myurl) to solve for it.

error with upload images in Attachments using Croogo and cakephp

Hope someone can help me!
I'm trying to upload an image in Attachments and insert image in a page using croogo.
And when I upload i keep getting the same error message:
Security Error
The requested address was not found on this server.
Request blackholed due to "auth" violation.
The images are saved in the uploads folder but i cant get them to display on the page.
Just add some line of code in your controller
public function afterFilter() {
if (($this->request->isPost() || $this->request->isPut()) && empty($_POST) && empty($_FILES)) {
$this->Security->csrfCheck = false;
}
}
OR
May be you forgot to define multipart in your form.
echo $this->Form->create('Something', array( 'type' => 'file'));
Hope this help you.

The url from an image via custom field (wordpress)

Am not even sure if this can be done but...
Ive added a feed from my forums to wordpress it works great but I need it to auto add the url of the image in a custom field from the images in the post (feed) first image would be fine as its only ahve a slider
Is there any way to do this?
Details
Ok I think I did not explain this very well so made a few screen shots
This is my slider at the minute with my
This is an imported post one other feed I was using
On this image you can see the custom field (which I have to fill in after every import)
Adding the image url into the custom field
and finaly a view of the slider working
This is what am trying to do (auto) so my feed from my booru / forums / 2 other of my sites and (2 other peoples) sites make my home page on a new site
Hope this explain it alot more
This uses the external Simple Pie library built into WordPress to fetch the feed, get the image url and create a new post for each item and save the image url as a custom field.
To activate the process we have to hook into wp_cron. The code below does it daily but it would probably be better to do it weekly to prevent overlap. Some overlap will probably occur so this still needs a way to check if we have already imported the image
First we need a function to save the custom field after the post has been created. This section comes from another answer I found on WordPress Answers.
Edit:
This needs to be wrapped in a plugin to schedule the cron event and the cron event was missing the action to make it fire.
Edit:
Final version below tested and it works but the feed the OP is getting is using relative url's so the domain name needs to be added somewhere in the output code.
<?php
/*
Plugin Name: Fetch The Feed Image
Version: 0.1
Plugin URI: http://c3mdigital.com
Description: Sample plugin code to fetch feed image from rss and save it in a post
Author: Chris Olbekson
Author URI: http://c3mdigital.com
License: Unlicense For more information, please refer to <http://unlicense.org/>
*/
//Register the cron event on plugin activation and remove it on deactivation
register_activation_hook(__FILE__, 'c3m_activation_hook');
register_deactivation_hook(__FILE__, 'c3m_deactivation_hook');
add_action( 'c3m_scheduled_event', 'create_rss_feed_image_post');
function c3m_activation_hook() {
wp_schedule_event(time(), 'weekly', 'c3m_scheduled_event');
}
function c3m_deactivation_hook() {
wp_clear_scheduled_hook('c3m_scheduled_event');
}
function create_rss_feed_image_post() {
if(function_exists('fetch_feed')) {
include_once(ABSPATH . WPINC . '/feed.php'); // include the required file
$feed = fetch_feed('http://animelon.com/booru/rss/images'); // specify the source feed
}
foreach ($feed->get_items() as $item) :
// global $user_ID;
$new_post = array(
'post_title' => $item->get_title(),
'post_status' => 'published',
'post_date' => date('Y-m-d H:i:s'),
//'post_author' => $user_ID,
'post_type' => 'post',
'post_category' => array(0)
);
$post_id = wp_insert_post($new_post);
if ($enclosure = $item->get_enclosure() )
update_post_meta( $post_id, 'feed_image_url', $enclosure->get_link() );
endforeach;
}

Resources