I am working on creating a PHP script to convert my HTML/CSS/PHP pages into a PDF. I have come across the class domPDF and I have uploaded all files to my server. I am trying to wrap my head around this and I have wrote this sample script to see if I could get it to work. The script is:
ob_start() ;
require_once("dompdf_config.inc.php");
$file= file_get_contents('http://www.yahoo.com');
$dompdf = new DOMPDF();
$dompdf->load_html_file($file);
$dompdf->render();
$dompdf->stream("sample.pdf");
And I am getting this error:
"Fatal error: Uncaught exception 'DOMPDF_Exception' with message 'Permission denied on <!DOCTYPE html> <html lang="en-US" class="y-fp-bg y-fp-pg-grad bkt701" style=""> <!-- m2 template --> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Yahoo!</title> <meta http-equiv="X-UA-Compatible" content="chrome=1"> <meta name="description" content="Welcome to Yahoo!, the world's most visited home page. Quickly find what you're searching for, get in touch with friends and stay in-the-know with the latest news and information."> <meta name="keywords" content="yahoo, yahoo home page, yahoo homepage, yahoo search, yahoo mail, yahoo messenger, yahoo games, news, finance, sport, entertainment"> <script type="text/javascript"> //Roundtrip rtTop = Number(new Date()); document.documentElement.className += ' jsenabled'; </script> <script type="text/javascript"> (function () { //refresh check var d=document,c=";\ in /home/public_html/Sandbox/include/dompdf.cls.php on line 329"
I think you want to use:
$dompdf->load_html($file);
rather than
$dompdf->load_html_file($file);
I'm not familiar with domPDF, but judging by the error message and the function's name, I assume load_html_file() expects a filename but you're giving it the contents of a HTML file. Try $dompdf->load_html_file( 'http://www.yahoo.com' ) or find a function that accepts a HTML string as a parameter.
Related
i am currently using html2pdf library in php for converting html page to pdf, it create pdf properly but google label in non clickable mode
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
Google
Google
</body>
</html>
Maybe its a stupied question but i didnt fild any answer for it,
If i have file with the html tag head tag&meta.. and I include him in all my web page, its effect the seo of the site?
I have file named "start_html.php" that have this code:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="description" content="bla bla.">
<meta name="keywords" content="bla bla">
<meta name="author" content="bla">
<title>bla bla</title>
<!-- CSS: -->
<link href="style.css" rel="stylesheet" type="text/css" media="screen" />
<!-- JavaScript -->
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
I start every page with this line: <?php include('start_html.php'); ?>
When i check for code error on w3c validator its says that i dont have those tags so the page is not coded good.
If the code from that file is not being displayed in your web page then obviously your PHP code is incorrect and that file is not being included. You need to make sure you have error reporting on and displaying all errors as this will catch this for you.
See this StackOverflow question for how to enable error reporting.
You should have these meta and title tags on all your pages, so including them from PHP is certainly not a bad idea.
However, if the W3 validator tells you these tags aren't there, you should check your output. Perhaps start by 'show source code' in your browser, and see if the tags appear there.
If you try to send your source file for validation, where you have:
<?php include('start_html.php'); ?>
Of course you will get the expected result - no tags, because the source file must be parsed and handled by PHP.
You can give a working link for validation, or copy output in your browser after execution, save the file and send it.
I have the following url to a file on my website:
http://www.foo.com/download/1
This downloads the file with the force_download function, using the download helper.
I've been requested to provide like buttons for these files which are displayed in a html table on one of the site's pages.
So, using facebook like button tool I've created the following button:
<div class="fb-like" data-href="http://www.foo.com/download/1" data-send="false" data-width="450" data-show-faces="false"></div>
However, because this is a direct link to the file, the correct open graph metadata is not being received e.g. the description is: "%PDF-1.3 %�ãÏÓ 1041 0 obj.."
So my question is how do I provide the open graph metadata for links to my downloads? For example:
<meta property="og:type" content="website" />
<meta property="og:url" content="http://foo.com/download/1" />
<meta property="og:title" content="Download 1" />
<meta property="og:image" content="bar.png" />
<meta property="og:description" content="File description" />
For the url that where you download the file itself you can't, since it's (probably) not an html page and even if it is, you wouldn't want to change the content of the file with your own metadata.
What you can try is use some kind of redirection on the URL used by both the clients downloading the file and the crawlers of facebook, and of course you will need an url for the real download too.
On your view sent for like and the users:
<!-- meta tags -->
<meta property="og:type" content="website" />
<meta property="og:url" content="http://foo.com/download/1" />
<!-- .... -->
<body>
<script>
// imaginary real download url url
window.location = '<?php site_url('download/real_download/'.$download_id);?>';
</script>
</body>
Facebook also doesn't seem to understand refresh meta tags, so you can write and avoid javascript usage:
<meta http-equiv="refresh" content="1;URL='<?php site_url('...')?>'">
Disclaimer: I didn't find any documentation from facebook that describe what their crawlers understand so there's no guarantee that any of these will keep working. I myself using a javascript solution for like a year now in a similar situation.
when we place flash files in our websites, it OFF-COURSE requires flash player on client machines, and prompts to install flash player...
is there some php code using which i can check weather there is flash player on the client machine and if not then instead of placing\embedding a flash file i place an images over there...
cuz in my specific case flash is not that much important... it is just for cosmetics, an animation... which i can replace by a gif or a simple jpeg doesnot matter...
but can i do it
swfobject can help with this.
You can just place the content to be shown if flash cannot be displayed in the code where the flash object should be, and point the script to it.
In code :
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>TestFlash</title>
<!--import the swfobject library /-->
<script type="text/javascript" src="swfobject.js"></script>
<!--Define which div (by ID) gets replaced if flash is present /-->
<script type="text/javascript">
swfobject.embedSWF("myContent.swf", "flash1", "300", "120", "9.0.0");
</script>
</head>
<body>
<h1>We are testing some flash</h1>
<hr />
<div id="flash1">
<!-- This stuff will show if flash is not present on the machine /-->
<img src="/img/image1.jpg" />
</div>
<hr />
<h2>this is a footer</h2>
</body>
</html>
See this article - http://www.adobe.com/devnet/flash/articles/fp8_detection.html
Adobe have got this sorted now so you can present alternative content / direct users to install flash / detect which version of flash user has and then install latest version if needed from your website without having to visit adobe site.
A little bit of work to get this in place so depends on how important it is that user is using flash or if the alternative content would work just as well. but for delivering flash content and being sure that the user will have correct version installed the Flash Detection Kit works great.
how about
<?
// Search through the HTTP_ACCEPT header for the Flash Player MIME type.
if (strstr($_SERVER['HTTP_ACCEPT'], 'application/x-shockwave-flash'))
{
$hasFlash = true;
}
if ($hasFlash)
{
// EMBED
} else {
// IMG
};
?>
I have problem with variable printing inside javascript.
variable htmlString printing not working: document.write(htmlString)
<?php $htmlString= htmlspecialchars(file_get_contents('http://google.com'));?>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<script type="text/javascript">
var htmlString="<?php echo $htmlString; ?>";
document.write(htmlString);
</script>
</body>
</html>
Edit:
Webpage source result: - Get all google.com inside htmlString the var not printed on the page(I cut the all content of htmlString because its very long)
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<script type="text/javascript">
var htmlString="<!doctype html><html><head><metahttp-equiv="content-type" cotring)";
document.write(htmlString);
</script>
</body>
</html>
Thanks
In order to pull remote pages with file_get_contents it requires fopen_wrappers to be enabled. If your host has this disabled and they allow cURL() I would go that route. cURL is also generally faster then file_get_contents, so that may be a deciding factor as well.
EDIT:
The problem you are having, particularly with google, is that it uses JS Code in the webpage. I just var_dump'ed the htmlString and it all displayed fine. But when putting it back into the JavaScript it went caput. The error that came back was an Unterminated String Literal (via Firefox's Error Console) on Line 8. Probably due to some single quotes etc. In my testing I tried htmlentities(), which worked and displayed the data to the browser. The section to change is:
$htmlString= htmlentities(file_get_contents('http://google.com'));
And it should work like you want it to.