In my laravel project I have the following myview.blade.php content:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>{{ __('menu.title') }}</title>
</head>
<body>
{{__('content') }}
</body>
But instead of rendering it into a http response, I want to be able to generate a pdf file out of it. Do you know How I can do this?
You will need to use a HTML to PDF library such as DOMPDF to do the actual conversion of HTML -> PDF
https://github.com/dompdf/dompdf
To get HTML from a blade view, you can do:
$html = view('path/to/view', ['vars' => 'hello'])->render();
I used many solutions, including DOMPDF, but on average the output generated by WKHTMLTOPDF is far better than DOMPDF.
WKHTMLTOPDF follow CSS rules more precisely.
Well, wkhtmltopdf is a command line program that accepts an html file as parameter, so you just have to install it and you are ready to go. It has package for Linux and Windows.
To the code
Generate the desired html from your view, like this:
$html = view('my_view', $arr);
Write the HTML to a temporary file:
$temp_file = tempnam(sys_get_temp_dir(), 'pdf_');
file_put_contents($temp_file, $html);
Run exec() on wkhtmltopdf:
$pdf_file = $temp_file . '.pdf';
exec('wkhtmltopdf ' . $temp_file . ' ' . $pdf_file);
Download your $pdf_file.
Be happy.
Related
html tag is not displaying the image and I am trying to display html file through php file.
HTML file:
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<div>
<img name="Userphoto" alt="Userphoto" src="img/50X50.png">
</div>
</body>
</html>
PHP file:
<form name="Dashboard">
<div class="frame1">
<?php include 'frame1.html'; ?>
</div>
</form>
HTML file is called into PHP file so path is consider of PHP one
Better way is to convert your html file into php and define your img path as
<img name="Userphoto" alt="Userphoto" src="<?php echo base_url(); ?>img/50x50.png">
Maybe your path to the image is not good since you included it in another file.
Let's say from your HTML file the path is "img/50X50.png", but if your php file is not in the same folder as your HTML code, the path isn't good.
Use absolute path and no more problems.
Have fun.
It is very likely that the image path is incorrect:
src="img/50X50.png
You can open Chrome Console to see the error message, which would indicate this, or provide another clue as to why the image hasn’t displayed.
I am trying to learn HTML DOM parsing at my localhost. But I crash right in the beggining.
1) I have made a new project
2) I have downloaded files from simplehtmldom.sourceforge.net
3) I have put this code inside my HTML project
<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<?php
// put your code here
$html = file_get_html('http://www.google.com/');
?>
</body>
</html>
4) I get this error, when I run the script:
Fatal error: Uncaught Error: Call to undefined function file_get_html() in C:\xampp\htdocs\PhpProject1\index.php:15 Stack trace: #0 {main} thrown in C:\xampp\htdocs\PhpProject1\index.php on line 15
Am I misunderstanding something here?
I think PHP DOM Parser Library No Longer Support to PHP latest Version so, You Need to Use cURL for that It will help you.
<?php
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "http://www.google.co.in");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
curl_close($curl);
print $result;
?>
Try this..!
file_get_html does not belong to php.
To use it you need to download and include PHP Simple HTML DOM Parser.
Then you can use it like this
include_once('simple_html_dom.php');
$html = file_get_html('http://www.google.co.in');
You need to include the DOM parser PHP library file in your HTML code using PHP include function.
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<?php
include_once('domparser.php');// use your file location
// put your code here
$html = file_get_html('http://www.google.com/');
?>
</body>
</html>
I am trying to serve an image to html via php script. But I am stuck and its not working :-(
Below is my image.php script
<?
$_GET['f'] = 'all_three.jpg';
$image = null;
$image = file_get_contents($_GET['f']);
header("Content-Type: image/jpeg");
echo $image;
?>
and below is my index.html
<html>
<head>
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
<meta content="utf-8" http-equiv="encoding">
<title>Page Title</title>
</head>
<body>
<img src="image.php" alt="Image" />
</body>
</html>
The link to image is "hard-coded" inside the php, all I am doing is calling "image.php" to serve that hardcoded image and it does not work! What am i doing wrong?
when I view->source after loading image.php, i can see the php script
Either:
You are depending on short tags, but your server is configured to not support them.
Their use is not recommended, stop using them. Use <?php instead of <?
Or:
Your server does not support PHP. Install PHP on it.
The error is on Content-Encoding, what's Text editor are you use ? Notepad 6.x ?
If yes please try:
Encoding > Encode in UTF-8 without BOM
BOM make php file have a more content and image doesn't accept another content else image
I have created a php static site and I use this following code to pick up a html file with the style sheets / javascript files in:
<?php require($DOCUMENT_ROOT . "styles.html");?>
This works on all browsers but when validating it, it comes up with the following errors:
document type does not allow element "link" here
<link type="text/css" href="style/style.css?v=2.1">
The html file does not have <html>, <head> or <body> as that caused different errors.
What is the best approach for this?
I am having problems with producing a Rectangle with GD - PHP. I am running XAMPP and programming on Netbeans IDE. I am new to PHP but some what fimilar with Perl.
<?php
header ("Content-type: image/png");
$newImg = ImageCreate(250,250);
$red = ImageColorAllocate($newImg,255,0,0);
ImageFill($newImg,0,0,$red);
ImagePNG($newImg);
ImageDestroy($newImg);
?>
Here is the out put.
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
</head>
<body>
‰PNG
IHDRúú²"È~PLTEˆÁÿ·¿~`IDAThíÁ‚ ÿ¯nH#¿ :F¨z’IEND®B`‚ </body>
</html>
You need to stuff the html into a seperate file, and use a <img> tag to load your php script, e.g.
<html>
<body>
<img src="yourimagescript.php" />
</body>
</html>
alternative, you COULD use data uris to embed the image directly into the html, but that brings about another set of problems, e.g.
<img src="data:image/jpeg;base64,<?php echo base64_encode(imagepng($newImg, '-')) ?>/>
If you want to use binary as source for image you need to use it as follows:
<img src="data:image/png;base64,data..."/>
or if you want use it as it is in your example you need to point image src attribute to your php script from example
<img src="path/to/script.php"/>
and path/to/script.php"/ displays only data and correct header
‰PNG
IHDRúú²"È~PLTEˆÁÿ·¿~IDAThíÁ‚ ÿ¯nH#¿ :F¨z’IEND®B‚