How can I render SVG with common PHP extensions? - php

I'm working on a project in PHP that needs to render dynamically-created SVG images to PNG (or GIF if not PNG) format. I know I can do this by invoking a SVG renderer like rsvg, or with an extension like ImageMagick, which isn't that common*.
Is there a "drop-in" style library that can render SVG using things like GD and DomDocument?
* Available in common webhosts and in packages like XAMPP.
To clarify, I already have the SVG itself generated, I just need it to be rendered server-size.

It shouldn't be too hard to do yourself. SVG is a fairly simple specification, so there shouldn't be too much guess work building a converter... The only difficult parts that I can see would be gradients, markers and filters. The rest should be relatively straight forward when looking at the gd functions available.
Obviously, the best would be to find a stand alone library, but if you can't you could always roll one yourself...

If you have Java on the server, you can use Batik's SVG Rasterizer, which comes as a standalone executable JAR. Call it using PHP exec.

Related

How can i convert a svg image to png image with Imagemagick when the svg file has embedded images?

With PHP i'm creating an image with embedded content (base64 encoded files). If i see it using Firefox, or downloading it and then opening it with Inkscape (www.inkscape.org), the image is fine!.
But when i try to convert it using imagemagick (using convert command or with Imagick support for PHP) the embedded image doesn't come in with the final result.
I don't know if there is a special command or configuration i'm missing. I'm not using any special setup. Just ...
convert image.svg image.png
Thank you very much for your answer.
ImageMagick's built-in SVG rendering is really pretty horrible. Don't use it if you can avoid it. I'd recommend using librsvg instead, either using the command-line rsvg tool, or possibly with the PHP rsvg extension.
(Librsvg's rendering isn't always perfect either, but it should be able to handle embedded images just fine. If you want even better rendering, you could always try using Inkscape from the command line.)
Is the embedded image in PNG or in Jpeg format? What converter is used in PHP? I tried on Windows with latest ImageMagick, which doesn't come with RSVG. I found a RSVG-Converter build for Windows, and, while it does a good job, it skips the Jpeg version of the image of a test file.
On the other hand, the built-in converter handles correctly both images, but does an awful job on the home image shown in the SVG tutorial.

Create Image from PDF using TCPDF

I am generating a PDF using the TCPDF library and would like to convert the pdf to an image. Anyone know if this is possible with TCPDF itself (I've looked over the code but doesn't look possible)?
Looking at previous questions on here it appears the best method is to use ImageMagick - is that still the case?
ImageMagick and Ghostscript would be the way to go to do that. I would assume that running the gs or convert commands from the command line is a bit more efficient since you dont have to load the imagemagick extension in PHP and it cuts the overhead from using abstraction classes for the extension.

Conversion of an Adobe PDF to the JPEG

Is there any ready PHP-based soultiuon for creation of an JPEG images from the Adobe's PDF files? Just like this: http://www.convertpdftoimage.com/
The ImageMagick program allows conversion between graphic formats, and it includes support for reading PDF files. Therefore it should be able to convert PDF to JPG. (indeed, googling for ImageMagick pdf to jpg gives plenty of results, with good usage examples)
Furthermore, there is a PHP extension for ImageMagick, so if you have that extension included in your PHP, then it should be pretty simple (you can check which extensions are included in a given PHP installation by using the phpinfo() function).
If you don't have that extension (and you can't install it), you can still use ImageMagick, using it's command-line interface via the PHP shell_exec() function, etc.
I don't know about PHP. But we use this application: Callas pdfToolbox. It's standalone on your UNIX server.
Apache provides a Java solutions: PDFBox
Not sure PHP is capable of rendering PDF's. Getting the text out of then however should be easy.

Converting doc, docx, pdf to HTML using PHP linux

i run a job search site, and i need to convert doc, docx and pdf files into HTML on linux CentOS server running php. People submit these files as resumes. So far, I found PHPDocx to be great at converting docx to html. But I am stuck at doc/pdf. PDFTOHTML gives error "bad color" when i run tests. As far as doc, i only found wvwave, which seems complex and bulky to install.
does anyone have any ideas on how to easily convert doc/pdf to HTML?
The only thing i can think of is FPDF.
It is intended for creating PDF files in PHP but it can also open PDF files.
Maybe you can use that as a base and develop some sort of toHTML function for it.
It is completely free to use and it has some extensions already.
It MIGHT help you.
http://www.fpdf.org
EDIT:
Thanks for the addition to my post in the comments to Pierre:
You can use fpdi: http://www.setasign.de/products/pdf-php-solutions/fpdi but the input pdf is just like an image.
I havent taken a look at it myself so far but this might help.
As far as .doc files go how about trying OpenOffice/LibreOffice, something like:
lowriter -convert-to html doc_file.doc –
As far as PDF goes, if the PDF is a graphical representation of text then you're out of luck, best you can do is try convert it to an image with ImageMagick, if it is a proper text it should easily convert.
There are various tools out there already to do this, such as http://dag.wieers.com/home-made/unoconv/, http://www.phpdocx.com/ (which you've already tried)
http://www.phplivedocx.org/2009/08/13/convert-docx-doc-rtf-to-html-in-php/ looks promising.
Or, you could install a portable version of libreoffice on your server which allows command line conversion
https://help.libreoffice.org/Common/Starting_the_Software_With_Parameters
I'm sure there'll be tutorials out there (on libreoffice support area)
To easily convert pdf to html, I would suggest pdf2htmlEX which produces outstanding HTML and is fast enough for runtime converting. You should first put some effort to optimize and build it for your system. There is simple build howto included on the project link.

Alternatives to ImageMagick for PDF downsizing

Having an issue with some PDF files not displaying properly in our iPad app. I have come to the conclusion that we are needing to standardize by "converting" PDF to PDF. I have successfully processed this using ImageMagick to convert the PDF to PNG (resized), and then pushing the PNG(s) back into a PDF. However, something within ImageMagick is making photos within PDFs display wrong. Same issue just converting a JPG or other graphic to PDF in ImageMagick. I solved that by taking the output of the converted ImageMagick file and converting it again using GD to PNG, then pushing it through our PDF converter.
So my question is this: What other PHP workflows would work with this, other than using ImageMagick for the conversion back to PDF? We are not opposed to a paid solution, we just need something that works. Our server runs centOS.
My gut instinct, other than yelling at whoever wrote the PDF reader that you're suffering with, would be to convert the PDF to PostScript using pdftops, then convert it back into a PDF using Ghostscript. You can enable a number of document compatibility options at that point, which may make it more digestible.
While this may have side effects, they should be minimal. PDFs are basically a wrapper around a PostScript document, and it looks like pdftops can not do utterly stupid things during the conversion process.
This may break or simply not work with advanced PDF features, like digital signatures or forms.

Categories