Converting HTML to images without installing anything - php

I want to create a functionality that converts HTML source code of some websites into jpg/png images. I also have to do that on external server - which means I don't know if it has all necessary libraries, and I don't wanna install there too much things, the less the better.
I have found two options:
- wkhtmltoimage
- cutycapt
But they both require dependencies to install, not to mention emulating server, which sucks.
Is there a way to do this without any external libraries? Thanks!

This is very much possible in linux using pbmtext. This tool converts any text into a pbm image file.
For you case,
curl -L google.com | pbmtext > google.pbm
However, beware that this will produce a very large file (several 100s of MB). So you will have to do some post-processing, like converting it into another format or resizing it.

You can use this Java Library, Html2Image.This simple Java library converts plain HTML markup to image and provides client-side image-map using HTML element and also supports different image formats such as GIF, PNG or JPEG image.
This library has very handy, simple and sober API's.
Common usage is this:
HtmlImageGenerator imageGenerator = new HtmlImageGenerator();
imageGenerator.loadHtml("<b>Hello World!</b> Please goto <a title=\"Goto Google\" href=\"http://www.google.com\">Google</a>.");
// OR you can fetch HTML from URL
imageGenerator.loadUrl("http://www.example.com/xyz.html");
imageGenerator.saveAsImage("hello-world.png");
imageGenerator.saveAsHtmlWithMap("hello-world.html", "hello-world.png");
Which will generate hello-world.png image of the HTML and hello-world.html file containing client-side image-map .
There are several others API's of this library which you can explore as per your needs.

Related

Rendering LaTeX to an image

I have read lots of questions and answers here but I haven't found a clear Question/Answer.
I need to create a really simple webpage which allows users to enter LaTeX and generate a PNG (or other lightweight image format). The webpage contains only a textarea and a submit button and it produces a link like "http://www.example.com/generated-images/cnl344l4jcxlj.png" with the image.
INPUT: already-written latex like
f(x)=\displaystyle\int_{-\infty}^x e^{-t^2}dt
OUTPUT: Link to the generated PNG or GIF image
The processing of the latex code and the generation of the image should happen in the server-side (so no JavaScript)
I'd like to do this in PHP (which I'm pretty good at), but if there are simple-enough libraries I could give it a shot in Perl.
A straightforward implementation in pure C could also work. A program which scanfs the input latex code and printfs "image generated at c:/program/image00000000001.gif"
Any free/opensource libraries that do any of this? Any advice in how to proceed? Any special concerns?
http://rinconmatematico.com/latexrender/pictures/35800007a15a3f0e39006dc63f04f1b5.gif
Try:
TeX2PNG - generate png image for given equation
MathJax - JavaScript library that find and render math equations in HTML
KaTeX - lighter and a little bit less capable alternative to MathJax
Look at dvipng. I wouldn't write a wrapper over it in C but in a scripting language.
If it's not a high-traffic website, you can try online rendering with sites like the Online LaTeX Equation Editor. In particular, this site allows you to put LaTaX directly into the URL like the following:
The URL being: http://latex.codecogs.com/gif.latex?\sum_k%20a_k

Generate PDF from HTML PHP

I want to generate PDF from a PHP file that includes HTML controls like textbox, and textarea. I attached CSS in the same. I tried FPDF, DOMPDF and TCPDF, but still I don't get exactly what I want. How do I pass HTML controls with PHP variables and CSS to these libraries?
mpdf is another option that you could try.
EDIT :
Found another solution for it, TCPDF is a FLOSS PHP class for generating PDF documents. Looks more dominating library.
"PRINCEXML" is a good library (not completely free now).
Others:
If your meaning is to create a PDF file from PHP, pdflib will help you (as some other suggested).
Else, if you want to convert an HTML page in PDF via PHP, you'll find
a little trouble outta here.. For three years I have been trying to do it as best as I
can.
So, the options I know are:
HTML2PS: same of DOMPDF, but this one convert first in .ps
(Ghostscript), then, in whatever format you need (PDF, JPEG, PNG). For
me it is a little better than dompdf, but I have the same speed problem.. Oh,
it has better compatibility with CSS.
Those two are PHP classes, but if you can install some software on the
server, and access it through passthru() or system(), have a look at
these too:
wkhtmltopdf: based on webkit (safari's wrapper), is really fast and
powerful... It seem like it is the best one (atm) for converting HTML pages to PDF on the fly, taking only two seconds for a three pages XHTML document
with CSS 2. It is a recent project. Anyway, the Google Code page is often
updated.
htmldoc: this one is a tank, it really never stops orcrashes... The project
seems to have died in 2007, but anyway if you don't need CSS compatibility
this can be nice for you.
** Thumbs Up For Strae.
If I understand your needs correctly I don't think any PHP-PDF class would do that.
Mostly you could insert only text and images to a PDF file, so if you would want something that looks like an HTML element you would need to insert it as an image.
Usually just putting HTML doesn't mean all your elements would stay intact in the PDF . (Different world, after all)
http://www.fpdf.org/ is the site having a great HTML-to-PDF class which work well. I am using it, but you have to first study its functionality and then start.

PHP create PDF invoice

Hi does anyone know how I can create a nicely formatted PDF invoice through PHP?
Ideally I'm looking for something with a header and then an itemised listing of the products with some sort of table around. After a quick Google I would be comfortable with generating a PDF but to try and style it nicely would be another thing altogether.
Thanks
I would recommend creating an html / css representation of what you want a PDF of and using that to generate the PDF. There are dozens of applications to handle the conversion, and there is a good thread with a lot of answers here: Convert HTML + CSS to PDF with PHP?
I use TCPDF (see http://www.tcpdf.org/) for this: its pretty capable and not too painful to get setup. I will say that depending on your data source you may have some issues. In my case my data is sourced from a SOAP interface to my accounting system and use CodeIgniter for my app, and I can do this:
$address = $extraclient->get_company_address();
// generate the PDF invoice
$this->load->library('pdfinvoice');
// set document information
$this->pdfinvoice->SetSubject("Invoice " . $data_invoice['code_invoice']);
// add a page
$this->pdfinvoice->AddPage();
$this->pdfinvoice->StartPageOffset();
// write the client's details out
$width = $this->pdfinvoice->GetPageWidth()/2;
$margins = $this->pdfinvoice->getMargins();
$this->pdfinvoice->SetFont('times', 'b', $this->pdfinvoice->bigFont );
$this->_row($width, array("From:", "To:"));
$this->pdfinvoice->SetFont('times', 'i', $this->pdfinvoice->smallFont );
$this->_row($width, array("MY NAME", $customer['name_contact']));
$this->_row($width, array($address['phone'], $customer['name_customer']));
$this->_row($width, array($address['street'], $customer['address1_street']));
$this->_row($width, array($address['city']. ", ".$address['state']." ".$address['zipcode'],
$customer['address1_city']. ", ".$customer['address1_state']." ".$customer['address1_zip
The full code is quite frankly too long to insert here, but you should get the idea, and you get fairly precise layout control.
You may wanna look at html2Pdf. It is a php class based on FPDF and it allows you to create PDF file from HTML. So just format your text using html and then create its pdf. Its very flexible and give greate control.
SourceForge Link
Checkout the php-pdf-invoice library from composer. The library is already designed for generating an invoice so it's easy and quick to integrate!
composer require quickshiftin/php-pdf-invoice
You can generate PDF files that look like this
There is a usage example on the README. Essentially you implement two interfaces, one for orders, another for order items. You can configure fonts and colors to your liking as well.
PDF generation tends to be tricky. As long as the invoice is relatively simple, I would recommend creating a template image file and use imagettftext to print strings over it (or any other library).
For image file to PDF conversion imagemagick tool suite can be used (as long as it is available on the server):
exec('convert -units \'PixelsPerInch\' -density 150 '.$filename.'.png '.$filename.'.pdf');
For me the whole approach worked smoothly.
Even though you can generate pdf documents programmatically, we have found out that it's best if you just generate an HTML page of the PDF document and use some kind of a tool / service to convert the HTML to PDF.
If you are looking for an app that you can host you can check these:
wkhtmltopdf - uses webkit to render html to pdf. works ok with some caveats.
PhantomJS - DISCONTINUED, I would not suggest using it.
Headless Chrome - Best of all but by itself it's not a solution. You need a client module according to your programming language environment.
In case you do not wish to handle the utility yourself, there are a lot of free / paid online services, some of them listed below:
PDF Layer (tested - good)
Restpack (tested - good)
DocRaptor
HTMLPDFAPI
HTML to PDF Rocket

PHP generate HTML convert to JPG then to PDF

I'm developing an app where the user adds items to a list. That list is stored in an array and passed to PHP with JSON.
The objective is to then create a PDF with all the values extracted from the user. The PDF is quite complicated. It includes images depending on what the user selects and the text varies depending on the images and the input data.
The first idea was to generate the pdf in php with one of those pdf libraries, but that's going to be a real hassle.
Then I thought of creating an html & css (much easier) and the convert it to PDF. But since the html & css are quite complex I don't think those pdf converters will work with this.
Then I thought I could convert the html to jpg and then to pdf.
It'll be much simpler if I could just use html but the output needs to be pdf.
What do you suggest?
Here's a post that discusses creating PDF files with PHP and the PDFLib extension.
Generate PDFs with PHP it's on sitepoint.
Or if you want to go from HTML to the PDF it looks like TCPDF might work.
You can try using FPDF
Then I thought of creating an html & css (much easier) and the convert it to PDF. But since the html & css are quite complex I don't think those pdf converters will work with this.
wkhtmltopdf to the rescue! If you are on a VPS or dedicated machine, it's probably the best (open source) HTML-to-PDF engine out there. It leverages Webkit, the rendering engine used by Google Chrome and Apple Safari, amongst others.
Otherwise, your only other options are going to involve drawing every aspect of the PDF or image yourself, "by hand" in your code.

How do I convert a PDF file to HTML in PHP?

How do I convert a PDF file to HTML in PHP? Is there any lib or web service? I mean free, thanks!
Google pdf2html, pdftohtml looks to be the only viable one. and it's based on a command line program, not PHP. so it may not be useful to you. Google is capable of converting, so there may be a way to do it with GDocs as well. though I'm not sure of that. At any rate, I hope this gets you on the proper path at least.
I've tried Poppler's pdftohtml command to convert PDF files to HTML files. Check it out on The HTML file output of Poppler is lighter when used but the output is not very accurate.
If you want accurate output you should use pdf2htmlEX I've converted complicated PDF files and got the best HTML output.
You can't.
PDFs are complex documents containing embedded fonts, vector graphics and layout information that cannot be represented in HTML in an automated way. You may be able to extract the TEXT of the document, but that's about it.

Categories