I have created a function where a user can go to this link and he can view his bill in PDF. It's working but the name is always . Here's my code:
function export_view($account_number_id, $subaccount_number_id){
$pdf = $this->pdf($account_number_id, $subaccount_number_id);
$filename = $pdf->name.'.pdf';
$path = public_path($pdf->path);
$response = Response::make(file_get_contents($path), 200);
$response->header('Content-Type', 'application/pdf');
$response->header('Content-Disposition', 'inline; filename="'.$filename.'"');
return $response;
}
I also did manual pdf filename to see if it's the issue but it's the same. When I download it in my local, the name is there. What could I be doing wrong?
I'm guessing your $this->pdf() method renders the pdf from a blade template?
You can set the name of the PDF document in the html title metatag:
<head>
<title>{{ $invoice->number }}</title>
</head>
Related
I am using barryvdh/laravel-snappy to generate pdf file. I have two image files 1. yourlogohere.png is in public/image/ folder and 2. logo2.png is in folder other than public i.e. storage/app/logo and to get this file I defined a route (www.example.org/logo/logo.png) and use following code to access it.
public function logo($filename)
{
$file = Storage::disk('local_logo')->get($filename);
$mime = 'image/png';
return (new Response($file, 200))->header('Content-Type', $mime);
}
Problem:
When I use following code to generate pdf from the html containing the first file, pdf contains the yourlogohere.png image
$snappy = App::make('snappy.pdf');
$html='<img src="http://www.example.org/images/yourlogohere.png" class="img-responsive" alt="Your Logo Here">';
$snappy->generateFromHtml($html, $path,[],$overwrite = true);
But when I do exact same thing for the second file, pdf does not render the image.(When I open the link http://www.example.org/logo/logo2.png in browser I get the image). What am I missing?
$snappy = App::make('snappy.pdf');
$html='<img src="http://www.example.org/logo/logo2.png" class="img-responsive" alt="Your Logo Here">';
$snappy->generateFromHtml($html, $path,[],$overwrite = true);
Thanks,
K
You can also do:
<img src="data:image/jpeg;base64,
{{ base64_encode(#file_get_contents(url('your.image.url'))) }}">
I think I got the what the problem is, the route to access the image is via auth, even when user is logged in while accessing the snappy, the wkhtmltopdf exe runs in a shell that is totally different session. Now the right fix would be to be embed the image in the html that is sent to snappy instead of the link, Which I am not sure how I will do? Any suggestions welcome there.
Update:
I as able to convert the image to data:image/png;base64, and embed it in html.
$html = view('mytemplate.default', compact('variable1', 'variable2'))->render();
/*Convert logo image to base64 before pdf conversion*/
//search for <img src="http://example.org/mytemplate/logo/logo1.png">" and replace the src with data:image/png;base64,
$search = '/(<img\s+src=["\'])([^"\']+)(\/mytemplate\/logo\/)(.*)(\.)(.*?)(["\']\s+[^>]+>)/';
$html = preg_replace_callback($search, function ($matches) use ($invoicedetail) {
$filename = $matches[4] . $matches[5] . $matches[6];
$file = Storage::disk('local_logo')->get('yourlogohere.png');
$mime = "image/png";
$mytemplate = MyTemplate::where('logo_filename', '=', $filename)->first();
if (!empty($mytemplate)) {
$file = Storage::disk('local_logo')->get($mytemplate->logo_filename);
$mime = $mytemplate->logo_mime;
}
$base64 = 'data:' . $mime . ';base64,' . base64_encode($file);
return $matches[1] . $base64 . $matches[7];
}, $html);
$pdf_filename = 'template' . $mytemlpate->id . '.pdf';
$path = storage_path('app' . DIRECTORY_SEPARATOR . 'templates' . DIRECTORY_SEPARATOR . $filename);
$snappy = App::make('snappy.pdf');
I'm following the instructions in the CodeIgniter Tutorial on the Static Page but the Header does not appear although the tab is labelled "CodeIgniter Tutorial" and the content of the Footer appears on the same line as the content of the Page.
This is the content of my Pages.php - application/controllers/Pages.php
<?php
class Pages extends CI_Controller
{
public function view($page = 'home')
{
if ( ! file_exists(APPPATH.'/views/pages/'.$page.'.php'))
{
// Whoops, we don't have a page for that!
show_404();
}
$data['title'] = ucfirst($page); // Capitalize the first letter
$this->load->view('templates/header', $data);
$this->load->view('pages/'.$page, $data);
$this->load->view('templates/footer', $data);
}
}
This is the content of my header.php - application/views/templates/header.php
<html>
<head>
<title>CodeIgniter Tutorial</title>
</head>
<body>
<h1><?php echo $title ?></h1>
This is the content of my footer.php - application/views/templates/footer.php
<em>© 2014</em>
</body>
</html>
This is the content of my routes.php - application/config/routes.php
$route['default_controller'] = 'pages/view';
$route['(:any)'] = 'pages/view/$1';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;
This is one URL http://localhost/MyProject/index.php/pages/view
And this is the result;
Home
Hello World! © 2014
And this is the other URL http://localhost/MyProject/index.php/pages/view/about
And this is the result;
About
Hello World! © 2014
I've checked every page numerous times, tried re-booting, and tried another browser, and tried CodeIgniter Forum, all to no avail.
Can somebody explain where I'm going wrong?
Edited
In the header.php file I've changed
<h1><?php echo $title ?></h1>
to
<h3><?php echo $title ?></h3>
which makes the word "Home" decreases in size. The only file I can find the word "home" is Pages.php
public function view($page = 'home')
If I change that word to "CodeIgniter Tutorial" I get a 404 error.
The IE tab is labelled "CodeIgniter Tutorial"
I'm unsure if my expectations are correct, but this is the result that I'm expecting (but with "© 2014" at the bottom of the page);
CodeIgniter Tutorial
Hello World!
© 2014
It seems like the files views/pages/about.php and views/pages/home.php are empty. Try writing something in them, for example:
views/pages/about.php
<i>This is the About page</i>
If the file exist, but is empty, you should get the result you're seeing, because this line will not render anything:
$this->load->view('pages/'.$page, $data);
If the files don't exist, you should get some 404 output - so that's a bit weird. It could happen if the file application/errors/error_404.php is empty.
I tested your project on my localhost and your pages controller is fine.
It just seems to be you need to either use p tags or div etc and then
use css style sheets etc.
But instead of
<em>© 2014</em>
</body>
</html>
Try
<p>© 2014</p>
</body>
</html>
Proof
I created a file at application/views/pages/CodeIgniter Tutorial.php
and changed
public function view($page = 'home')
to
public function view($page = 'CodeIgniter Tutorial')
in Pages.php
and changed
<em>© 2014</em>
to
<p><br><br><em>© 2014</em></br></br></p>
in footer.php
I can achieve close to my expectations and now realise my expectations were far too much for a simple piece of coding.
I read this officail Joomla article about Microdata: http://docs.joomla.org/Microdata
I tried put this meta element in the head section of my Joomla website:
<meta itemprop="name" content="title of your content">
By this code:
$scope="itemprop";
$property="name";
$content="title";
JMicrodata::htmlMeta($content, $property, $scope = '', $inverse = false);
But no success! Can somebody tell me whats the wrong?
Solutions
To add this meta tag in you <head> section of a Joomla website:
<meta itemprop="name" content="title of your content">
You can use one of the following solutions
1) Add this code in the <head> section:
echo JMicrodata::htmlMeta($content = 'title', $property = 'name');
2) In whatever part of your code/file you want:
$microdata = JMicrodata::htmlMeta($content = 'title', $property = 'name');
$document = JFactory::getDocument();
$document->addCustomTag($microdata);
Documentation help
JMicrodata::htmlMeta() is used for output microdata semantics in a meta tag, this method does not add the meta tag in the <head> section.
I see you use $scope="itemprop", which is wrong, the scope is used to specify the Type of microdata, here you can find the full list of the available Types http://schema.org/docs/full.html
I suggest you to use an instance of JMicrodata, this way you don't need to worry that microdata is displayed properly.
$microdata = new JMicrodata('Article');
echo $microdata->content('title')->property('name')->display('meta');
In the <head> section add
<?php
$property="name";
$content="title";
echo JMicrodata::htmlMeta($content, $property, '', false);
?>
That will definitely get you the metadata.
Elsewhere if you did
$property="name";
$content="title";
$microdata = JMicrodata::htmlMeta($content, $property, '', false);
$document = JFactory::getDocument();
$document->addCustomTag($microdata);
That should do the trick.
im making currently making my thesis about a record management of our university secretary.. in which all papers inside the office will be scanned and uploaded in the system.. i am using codeigniter..one of the feature in my system is to view the pdf file in other window of the browser. but my problem is, when i click the title. only blank page will be displayed in the other tab.. can you help me solve this one?? here is my code
controller:
function viewMinutesFile(){
if(isset($_GET['id'])){
$id = $_GET['id'];
$file = $this->minutes_model->getFile($id);
$fp= fopen($file->path, "r");
header("Cache-Control: maxage=1");
header("Pragma: public");
header("Content-type: application/pdf");
header("Content-Disposition: inline; filename=".$file->filename."");
header("Content-Description: PHP Generated Data");
header("Content-Transfer-Encoding: binary");
header('Content-Length:' .filesize($file->path));
ob_clean();
flush();
while (!feof($fp)){
$buff = fread($fp,1024);
print $buff;
}
exit;
}
}
code to open the file: this is my syntax to be clicked by the user so that pdf file will be open in the new tab
File
index.php/admin/viewMinutesFile?
id=" target="_tab">
try this one with a static url. no need any extra words for that.
Show My Pdf
New Update
if its work for you then fetch pdf name from database and put the name in the view like
Show My Pdf
now in the controller
$this->load->helper('download');
if($this->uri->segment(3))
{
$data = file_get_contents('./file_path/'.$this->uri->segment(3));
}
$name = $this->uri->segment(3);
force_download($name, $data);
well, you could add a link to file with target="_blank", like
<a href="<?php echo base_url(). 'your_controller/viewMinutesFile'; ?>" target="_blank">
View Pdf
</a>
and in controller function:
function viewMinutesFile(){
....
$file = $this->minutes_model->getFile($id);
$this->output
->set_content_type('application/pdf')
->set_output(file_get_contents($your_pdf_file));
}
you can try this on your view :
Filename
and on your controller, you can try this, because this is works for me :
function viewfile(){
$fname = $this->uri->segment(3);
$tofile= realpath("uploaddir/".$fname);
header('Content-Type: application/pdf');
readfile($tofile);
}
hope this might help you...
Just create a link to a blank page and use this code in your controller:
public function myPdfPage(){
$url = base_url('assets/your.pdf');
$html = '<iframe src="'.$url.'" style="border:none; width: 100%; height: 100%"></iframe>';
echo $html;
}
Enjoy!
There is no any problem with your code you can open easily on next tab, like other pages only difference you have to change header description and it is make sure on your browser pdf reader add-ons are available, otherwise it will give you option to download.
You may just follow this.
<?php echo form_open_multipart('your_controller/your_function','target="_blank"') ;?>
//other input fields
<?php form_close();?>
I'm just wondering if the following is possible:
I don't know anything about JQuery and I don't know how to make my own javascripts yet.
When a user logs into my website, they'll get a page that says "login completed".
Under that sentence, I would like to get a sentence that counts down the seconds until they are redirected. Is there a simple way to do this?
I already have a countdown function:
When the function is first called, the view 'view_login_success' is loaded and the $data[] array is passed to it. $data['sec'] holds the seconds remaining that should be printed on the screen.
When the do-while loop is over, the user should be redirected to the homepage.
What happends is, the 'view_login_success'-view isn't loaded at all and after 5 seconds, the user is redirected to the homepage.
function timerIn() {
$now = time();
$this->load->view('view_login_success', $this->data);
do {
if (time() - $now != 0) {
$this->data['sec'] = $this->data['sec'] - 1;
$this->load->view('view_login_success', $this->data);
$now = time();
}
} while ($this->data['sec'] != 1);
$this->data['sec'] = 5;
redirect('user/start');
}
Here is the view:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Login geslaagd</title>
<?php $this->load->view('templates/header2'); ?>
</head>
<body>
<h2 style="text-align: center;"><?php echo strtoupper(substr($this->session->userdata('gebruikersnaam'),0,1));
echo substr($this->session->userdata('gebruikersnaam'),1); ?>, U bent ingelogd!</h2>
<p>U word doorverwezen: <?php echo $sec; ?> seconden...</p>
</body>
<?php $this->load->view('templates/footer'); ?>
If there's a way to do this in JQuery of Javascript, then please explain to me what I should do to get it to work.
Thanks in advance! :)
That's not how php works.
If you want the page to redirect after 5 secs you have to do so in the JS code.
You redirect the user without sending the view to the client.
Right now you are only waiting 5 seconds to redirect. So:
Show the view
The JS will have the timer and then redirects.
The javascript code would be something like this:
setTimeout(function(){
window.location.href = 'theURLyouwant';
},5000)
OR you could use the ol' meta tag
<meta http-equiv="refresh" content="5; URL=theURLyouwant">
Use code-igniter to solve this problem. Use this reference class output. Output class will be loaded automatically. You don't need to load this. Use this function to set header.
$this->output->set_header('refresh:time-to-redirect; url='.site_url("your-url"));
For more detail about this class.