Displaying pdf in laravel - laravel

I'm creating a site where an author uploads a pdf file. The way I've created my site is there is a page where story titles is shown, if a person clicks on the title it uses the story slug to go to the story, but I want a pdf to be displayed when the the title is clicke d. I'm not sure where to start. I have tried barryvdh/laravel-dompdf, but it doesn't have a "partial" section to display a pdf. It only allows for the pdf to be viewed as a whole page. I hope I made sense.
Something similar to this Google Books

Try this way :
$filename = 'test.pdf';
$path = storage_path($filename);
return Response::make(file_get_contents($path), 200, [
'Content-Type' => 'application/pdf',
'Content-Disposition' => 'inline; filename="'.$filename.'"'
]);
Try this html object tag :
<div>
<object data="test.pdf" type="application/pdf" width="300" height="200">
alt : test.pdf
</object>
</div>

Related

Vue js and laravel keep an uploaded image permanently

I wrote a code to upload an image file and that is already working, I also can save the image url in my database. The problem is the image is not loaded permanently. Which means I can see the image after I uploaded it, but the moment I save it, the url is not valid anymore. Does anyone know how to fix it? I mean store the image permanetly on my project. I want users to upload and use their own images? Thank you for youre help.
uploading in template
<input style="margin-left: 35px" type="file" #change="onFileChange" />
Displaying the image
<img v-if="data.image" :src="data.image" style="max-width:450px;vertical-align:middle;margin:0px 5px 5px"/>
script method
onFileChange()e {
const file = e.target.files[0];
this.data.image = URL.createObjectURL(file);
},```
You could do something like this in your Controller:
$request->validate([
'image' => 'required|image|mimes:jpeg,jpg,png,gif|max:2048',
]);
$imageName = time().'.'.$request->image->extension(); //What you store in DB
$request->image->move(public_path('images'), $imageName); //Put the image in a public folder
And from there you could access it via URL.

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

Laravel - Set page title when using Response::make to view pdf file

I am trying to set the page title in Laravel to be the name of the file that is being viewed in the browser. I have looked through the documentation and questions on stack overflow and cannot seem to find the solution. The page title keeps defaulting to the primary key
public function download(SomeModel $document)
{
if(Storage::disk('s3')->exists($document->file_path)) {
return \Response::make(Storage::disk('s3')->get($document->file_path), 200, [
'Content-Type' => 'application/pdf',
'Content-Disposition' => 'inline; filename="'. $document->custom_file_name .'"'
]);
}
return view('errors.404');
}
How can I make it to where the <title></title> of the page is using the $document->custom_file_name as opposed to being the id.
You need to set the Title metadata on your pdf document, then the browser will interpret it as the title automatically.
http://www.w3.org/TR/WCAG20-TECHS/PDF18.html

Using codeigniter to execute code from another file

I am new to CodeIgniter and I am used to the old school php scripting so I'll need some help with this:
I want to include a Captcha system in one of my forms.
According to its documentation, to generate an image, you need to do it like this:
<img id="captcha" src="/securimage/securimage_show" alt="CAPTCHA Image" />
I downloaded the files, but where do I put them? And how do I use Codeigniter to call the securimage_show.php file? And output its contents into the src attribute of the image?
When adding Captcha in Fuel (a codeigniter based cms), I've put the php file that generates the Captcha image in the folder where you put images, then link to it the same way you link to an image:
<?php echo img(array('src'=>'image_show.php', 'alt'=> 'CAPTCHA Image')); ?>
Perhaps not the nicest solution, but it works.
Alternatively, just use a Captcha plugin written specifically for codeigniter, such as the NuCaptcha CodeIgniter plugin, http://docs.nucaptcha.com/plugins/codeigniter.
Codeigiter have a captcha helper.
First, you want to create a folder where you will be able to store your captcha images and give this folder permissions to perform read/write operations. In this case, I've created captcha folder in root of my codeigniter instance.
Then, we want to load captcha helper:
$this->load->helper('captcha');
Let's initiate instance of captcha with our settings (you can do it either in Controller or View with your form):
$rand_string = strtoupper(random_string('nozero', 4));
$settings = array(
'word' => $rand_string,
'img_path' => './captcha/',
'img_url' => base_url() .'captcha/',
'img_width' => '250',
'img_height' => 35,
'expiration' => 7200
);
$cap = create_captcha($settings);
$this->session->set_userdata('captchaWord',$cap['word']);
Please note, that I'm keeping generated captcha word in my session whenever I create it (for instance on page refresh). This way I can compare original captcha word with input from my form. Then, I will display generated captcha image with input field somewhere in my form (View):
<form id="my_form">
<input type="text" name="captcha" value=""/>
<?= $cap['image']; ?>
</form>
Now, all I have to do, is to compare input received from my_form with actual captcha value (in my controller, where I handle form submission):
$userCaptcha = $this->input->post('captcha');
$actual_word = $this->session->userdata('captchaWord');
if( strcmp(strtoupper($userCaptcha),strtoupper($actual_word)) == 0 ) {
// input and captcha are the same
}

Showing attached images of certain size in Wordpress posts

I am currently using WordPress to build my portfolio website. The site consists of:
a homepage showing "project" posts (using post categories).
an about us page
a projects page (very similar to homepage) showing thumbnails and titles of recent projects.
a project detail page (this is a click through from either the homepage or projects page and shows detail on a particular project.
On the project detail page I have an image slider which I want to show the work associated with that project. I have used the following function call to return all images associated with the post:
<?php display_images_in_list('medium'); ?>which calls the following function:
function display_images_in_list($size = thumbnail) {
if($images = get_posts(array(
'post_parent' => get_the_ID(),
'post_type' => 'attachment',
'numberposts' => -1, // show all
'post_status' => null,
'post_mime_type' => 'image',
'orderby' => 'menu_order',
'order' => 'ASC',
))) {
foreach($images as $image) {
$attimg = wp_get_attachment_image($image->ID,'medium');
echo "<div class='slide'>".$attimg."</div>";
}
}
}
Even though I have specified I would like medium images returned my slider still features the thumbnail image which I uploaded to show on another page.
How can I show only medium images?
when you upload image as attachment to post wp creates thumbnails. You cannot upload a thumbnail. If you upload big image, and thumb image both will be shown. You need to upload only big images and than it will show medium size as you wish.

Resources