converting html text to an image with php - php

what's the best way to convert a text embedded in a html tag to an image using php keeping the style written in the html tag ? for example :
convert :
<span class="Apple-style-span" style="font-size: xx-large;"><font class="Apple-style-span" color="#F4A460">Stack </font><font class="Apple-style-span" color="#800000">Overflow</font></span>
into :
is there any class for it ? or should I explode it and read the tags one by one ? any suggestion ?

Might want to have a look at Painty. Although it isn't exactly what you're looking for because you'll have to feed it an array of options, it should be a good resource on which you can expand.
Not sure if you also want to render the font(s) being used in your HTML snippet, but if you do, you would also have to get all the commonly used web-fonts and put them all in a folder from where the script can read.
Hope this helps.

With PHP GD Library support, yes:
http://visionmasterdesigns.com/tutorial-convert-text-into-transparent-png-image-using-php/ (font/size technique included)
http://corpocrat.com/2009/06/23/php-script-to-convert-textemail-address-to-image/

Check this one out
http://code.google.com/p/wkhtmltopdf/downloads/list
The project is centered around html to pdf using the webkit engine, but there are also binaries and source for html to image. It's an external binary though, so might not be useful to you in your use-case.
Otherwise I would look into imagemagick.

Related

How to extract only text from HTML string with PHP?

I want to extract only text from a php string.
This php string contains html code like tags or etc.
So I only need a simple text from this string.
This is the actual string:
<div class="devblog-index-content battlelog-wordpress">
<p><strong>The celebration of the Recon class in our second </strong>BF4 Class Week<strong> continues with a sneaky stroll down memory lane. Learn more about how the Recon has changed in appearance, name and weaponry over the years…</strong></p>
<p> </p>
<p style="text-align:center"><img alt="bf4-history-of-recon-1" class="aligncenter" src="http://eaassets-a.akamaihd.net/battlelog/prod/954660ddbe53df808c23a0ba948e7971/en_US/blog/wp-content/uploads/2014/10/bf4-history-of-recon-1.jpg?v=1412871863.37" style="width:619px" /></p>
I want to show this from the string:
The celebration of the Recon class in our second BF4 Class Week continues with a sneaky stroll down memory lane. Learn more about how the Recon has changed in appearance, name and weaponry over the years…
Actually this text will be placed in meta description tag so I don't need any HTML in meta tag.
How can I perform this? Any ideas and thoughts about this technique ?
You may try:
echo(strip_tags($your_string));
More info here: http://php.net/manual/en/function.strip-tags.php
Another option is to use Html2Text. It will do a much better job than strip_tags, especially if you want to parse complicated HTML code.
Extracting text from HTML is tricky, so your best bet is to use a library built for this purpose.
https://github.com/mtibben/html2text
Install using composer:
composer require html2text/html2text
Basic usage:
$html = new \Html2Text\Html2Text('Hello, "<b>world</b>"');
echo $html->getText(); // Hello, "WORLD"
Adding another option for someone else who may need this, the Stringizer library might be an option, see Strip Tags.
Full disclosure I'm the owner of the project.

Read the content of a PDF with PHP?

I need to read certain parts from a complex PDF. I searched the net and some say FPDF is good, but it cant read PDF, it can only write. Is there a lib out there which allows to get certain content of a given PDF?
If not, whats a good way to read certain parts of a given PDF?
Thanks!
I see two solutions here:
converting your PDF file into something else before: text, html.
using a library to do so and bad news here, most of them are written in Java.
https://whatisprymas.wordpress.com/2010/04/28/lucene-how-to-index-pdf-files/
What about that ?
http://www.phpclasses.org/package/702-PHP-Searches-pdf-documents-for-text.html
ps: I don't test this class, just read the description.
$result = pdf2text ('sample.pdf');
echo "<pre>$result</pre>";
How to get “clean” text :source code pdf2text
http://webcheatsheet.com/php/reading_clean_text_from_pdf.php

How to save a the content into the file with neat alignment?

I am having a textbox, in that i have loaded a xml file.
After editing and saving the xml content into the xml file, the content is not in the right formate.
While loading again, its not in the xml format
How to save a the content into the file with neat alignment?
Please help me
For ExampleI need to save like the following
<section>
<value>a</value>
<value>b</value>
</section>
But after saving its looks like
<section><value>a</value><value>b</value></section>
Thanks,Praveen J
As Gordon says your question makes no sense - the XML fragment is still "well-formed" (but its far from complete) so it is in the right format.
Do you mean you want to preserve the format it was submitted in? In which case output it using <pre>...</pre> tags. OTOH there are standard tools out there which wil format XML according to specific standards - e.g. geshi
C.
I think is issue is that it doesn't preserve whitespace, so opening the xml file later shows it all in a single line as opposed to spaced/tabbed as originally created.
You can try white-space: physical as a CSS attribute on your textarea. Alternatively you can try adding the attribute/value pair "wrap=hard" to your textarea declaration. Both methods should preserve whtepace.

'echo' or drop out of 'programming' write HTML then start PHP code again

For the most part, when I want to display some HTML code to be actually rendered I would use a 'close PHP' tag, write the HTML, then open the PHP again. eg
<?php
// some php code
?>
<p>HTML that I want displayed</p>
<?php
// more php code
?>
But I have seen lots of people who would just use echo instead, so they would have done the above something like
<?php
// some php code
echo("<p>HTML that I want displayed</p>");
// more php code
?>
Is their any performance hit for dropping out and back in like that? I would assume not as the PHP engine would have to process the entire file either way.
What about when you use the echo function in the way that dose not look like a function, eg
echo "<p>HTML that I want displayed</p>"
I would hope that this is purely a matter of taste, but I would like to know if I was missing out on something. I personally find the first way preferable (dropping out of PHP then back in) as it helps draw a clear distinction between PHP and HTML and also lets you make use of code highlighting and hinting for your HTML, which is always handy.
The first type is preferable, exactly for the reasons you mentioned.
Actually, echoing out whole chunks of html is considered bad practice.
No, there's no performance increase that would be visible.
Sometimes its just simply easier to output content using echo (for example, when inside a while or for loop) than to close the php tag.
I think there's a preprocessor which converts the same form into the second. That's what happens in ASP.NET, anyway. And in both ASP.NET and classic ASP, loops can actually stretch across raw-HTML regions.
There's no performance difference at all.
Just the style that produces the most readable code. Depending on the actual situation that can be either of the two.
But mixing HTML and PHP should be avoided where possible anyway. THis can be accomplished by using a template system for your views.

rtf format to pdf

Is there any way to convert rtf format to pdf using PHP?
Thanks
If you want to stick with pure PHP, you can probably use HTML as an intermediary:
Convert RTF to HTML
http://freshmeat.net/projects/rtf2htm/ , http://www.phpclasses.org/package/1930-PHP-RTF-to-HTML-converter-with-latin-character-support.html
Optionally: clean up the HTML
http://htmlpurifier.org/
Convert HTML to PDF
http://dompdf.github.io/
You can use OpenOffice command line interface for that. Check my answer to a similar question.
Ted is the tool you're looking for. Ted brings also a script called rtf2pdf.sh you can execute by PHP to create a PDF file.
You should try out livedocx livedocx.com . The latest Zend Framework 1.10 has a ready built module to help you out. You can read more about it at this place http://www.phpfreaks.com/tutorial/template-based-document-generation-using-livedocx-and-zend-framework

Categories