I've got an idea for a site that would generate png or jpeg screenshots of webpages on the fly. The end user would never see the pages, but the HTML would be turned into a screenshot instead and the end user would see that screenshot.
How can I get started on this? I guess what I'm looking for is some kind of PHP function that takes the HTML as an argument and then produces an image file in a specified location.
As far as I know, PHP does not do this.
You can, however, find a solution using external tools.
Here is how I would do it
Generate the HTML
I would pass this HTML to a external tool using exec. There is, for instance this one.
Then, display the generated picture
http://www.zubrag.com/articles/create-website-snapshot-thumbnail.php
Related
I see some websites allow users download our source include html file, css file, js file ... and almost. I don't want to do this for my website. What should I do? Thank for wathching!
P/S: If you can, please show me this approach with Zend. I'm using Zend 1.9.6.
You cannot restrict the download of resources. The browser needs to download them in order to process them, if you restrict them from being downloaded the browser wont be able to access them as well.
That is impossible. The browser need the source code to display your site, in the same way you can't prevent the user to download an image if you show it to them. The best you can achieve is obfuscate your CSS and Javascript to a hard to read scrambled code, using YUICompressor, for example. But someone determined will always be able to decipher your code logic...
As said its impossible to hide js or css files but what you can do is minify(compress) them which will make it harder to interpret by user and making your site load faster at the same time .
Check this implementation of minfy library with ZF , it provides css,js view helpers to automate the compression .
http://hobodave.com/2010/01/17/bundle-phu-compress-your-js-css-in-zend-framework/
If their browser can't read your HTML, how can it display your page?
They won't be able to read your PHP (assuming that your server is set up to parse PHP correctly), but they will always be able to read the HTML output.
I want the option of converting HTML to image and showing the result to the user. I would be creating an $html variable with PHP, and instead of displaying using echo $html, I want to display it as an image so the user can save the file if they needed to.
I was hoping there would something as simple as $image = convertHTML2Image($html); :p if that exists?!
Thanks!!
As #Pekka says, the job of turning HTML code into an image is the job of a full-blown web browser.
If you want to do this sort of thing, you therefore need to have a script that does the following:
Opens the page in a browser.
Captures the rendered page from the browser as a graphic.
Outputs that graphic to your user.
Traditionally, this would have been a tough task, because web browsers are typically driven by the user and not easy to automate in this way.
Fortunately, there is now a solution, in the form of PhantomJS.
PhantomJS is a headless browser, designed for exactly this kind of thing -- automated tasks that require a full-blown rendering engine.
It's basically a full browser, but without the user interface. It renders the page content exactly as another browser would (it's based on Webkit, so results are similar to Chrome), and it can be controlled by a script.
As it says on the PhantomJS homepage, one of its target use-cases is for taking screenshots or thumbnail images of websites.
(another good use for it is automated testing of your site, where it is also a great tool)
Hope that helps.
This is not possible in pure PHP.
What you call "converting" is in fact a huge, non-trivial task: the HTML page has to be rendered. To do this in PHP, you'd have to rewrite an entire web browser.
You'll either have to use an external tool (which usually taps into a browser's rendering engine) or a web service (which does the same).
It is possible to convert html to image. However, first you must convert to PDF. see link
You may have a look at dompdf which is a php framework to convert a html file to a pdf.
use WKHTMLTOPDF. works like a charm. it converts to any page to PDF ..
a jpeg can be obtained by performing later operation.
http://code.google.com/p/wkhtmltopdf/
I need to convert the html receipt to pdf file. in html file I also have php information.
How can I provide such a thing?
could you suggest me with codes.
This is going to be a listing question, but we've used DOMPDF as #skippy suggested, and rejected it because it is a REAL troublemaker when tables surpass a page.
We've moved to http://code.google.com/p/wkhtmltopdf/ , which is -in my opinion- way better. It's being used in a production environment sending out lots of pdfs per day. It uses the webkit rendering enginge (you know, the one that chrome uses) and is therefore awesome :)
Have you taken a look at the DOMPDF project ? It simply rockz...
If that is not enough, you may want to browse StackOverflow itself :
Convert HTML + CSS to PDF with PHP?
Try using this to automagically convert html to pdf:
www.tcpdf.org
Try this
http://code.google.com/p/wkhtmltopdf/
it works like a charm. :)
My company has a product called DocRaptor that converts HTML to PDF using an HTTP POST request.
DocRaptor is built using Prince, and produces higher quality results than similar programs.
Here's a link to DocRaptor's homepage:
DocRaptor
I am using this code there is no need to installation of any thing .
http://sourceforge.net/projects/html2fpdf/
We are downloading images to our computers when we open new webpages. For example: If a webpage has an image(image.jpg), our computer downloads it while we are surfing that page.
Some webpages are using ajax methods. For example: You don't see an image on the page's source codes, however your computer downloads an image. Because, if you click a link on that page, ajax will be showing that image...
Let me show an example:
<div id="ajax_will_load_image_here"></div>
Okay, how can php curl see (or download) that image? Curl can't see that image when I try to use preg_match function. Actually there is an image. I want to download that image by using php curl. Any advice?
If i understand the question correctly there is no convinient way of doing that.
Your crawler/spider would have to parse the website and evaluate javascript.
There are libraries for that but support is very limited.
There are however methods where an actual browser is used to evaulate the page (without displaying it but setting proper environment variables like resolution etc).
Then the generated source including javascript dom modifications is available.
This is for example how the google search previews are generated.
But if you require user interaction it gets pretty specific and complicated.
I am sorry to dissapoint you, but using curl and preg metch the old school way we used to when javascript was not yet so common wont work.
However for most legit use cases this is more than sufficient and websites are today more and more designed to be non-javascript compliant. Especially the content for crawling purposes. It is a must in search engine optimization, and which website doesnt want that?
We want to display a pdf-file on a webpage.
From what i can think of i see two possible solutions, displaying the file with some kind of pdf reader(maybe in flash?) or converting the pdf-file to html before displaying it.
How would you proceed to solve a problem like this?
Which would be the preferable method?
Well, there's always a third way: serve the PDF itself and leave the rest to the visitor.
For public websites, you can improve the user experience and reduce bandwidth overhead by embedding your PDF documents in your pages using one of the document sharing services such as:
http://www.scribd.com
http://www.docstoc.com
I should also add that scribd also has an API for uploading documents (and more).
If you absolutely need to display the PDF in the browser, you can use FlashPaper. It installs on Windows as a printer, and lets you convert any kind of document to SWF, which you embed in your HTML.
I've used it in several projects, but it's not an ideal solution. From the user standpoint, the best thing is to be able to download the PDF and read it with her favorite PDF viewer.
Try using the embed or object html tags.
http://blog.flashcolony.com/?p=244
Personally I wouldn't bother with that, and just rely on the user to have a proper pdf reader. If you go for a flash (or silverlight?) solution, you're imposing another requirement to the user to cover up the first one. On the other hand, converting PDF to HTML isn't all that easy, just look at how the output from Gmail's 'view as html' functionality looks.
As said, and as others already posted while I'm writing this I am sure, is to not bother and just let the visitor deal with having something to read pdf with ;-)
A solution not mentioned by others is to rasterize the PDF (say, via ghostscript) and serve the resulting image as PNG, JPG, etc. You have to choose the resolution (perhaps 72 dpi) and you have to understand that the document will become much less readable, especially to sensory impaired visitors.
Create a PHP file like this: I'm calling this first php file "firstfile.php"
<?php
header('Content-type: application/pdf');
$file='yourpdffile.pdf';
#readfile($file);
?>
Then create another PHP file and use iframe to get your desired PDF file. Sample code is below
<iframe src="http://localhost/Domainfolder/firstfile.php>" height="400px" width="750px">
</iframe>
This should do the trick unless you don't reference the links well. Enjoy ;)