Published 2003-12-14 11:30:25

As part of the Membership Billing system I'm working on the client had a requirement for
  • some nice mailing envelopes
  • a very pretty bill to be sent out to the members.
Read on to see how I ended up using sodipodi, and producing sexy PDF's using SVG templating.


I had originally developed a line printer type reporting system [Gtk_TextReport] to do this, which had a crude interface to design text reports. Unfortunatly as we came to production, this never really delivered on it's promise. (it was designed to mimic the old DOS application they where using). So I had to explore alternatives.

FPDF [www.fpdf.org] is quite a nice library for creating PDFs, however, the thought of hardcoding up all the locations in PHP, for all the text on both applications horrified me.

My first idea was to consider writing a HTML to PDF generator, based off of Flexy's [HTML_Template_Flexy] 's tokenizer, which is extremely good at parsing loose HTML and XML. but after spending half a day looking at this, and the issues, decided that it was just too messy. The amount of data that I needed to overlay as extra attributes pretty much exceeded the amount of HTML. The other snag is that the Tokenizer in Flexy is designed to be used cached parser, as it is by the Flexy compiler (it's a bit slow for large html files).

So Idea no.2 was to use SVG, Something I'd been thinking of using for a number of projects (mostly with PHP-GtkCanvas), What it needed was to have some kind of markup language, similar to Flexy, eg {xxxx} reads variable $page->xxxx, and {xxxx()} calls $page->xxx(), The resulting package [XML_SvgToPdf] was reasonably simple, taking about 2 days to write

It uses [XML_Tree_Morph], a simple wrapper for XML_Tree, to morph the XML in an svg file into an object type based on the tag, eg. <tspan ...> gets mapped to XML_SvgToPdf_Tspan, when the parser finds the closing XML tag, the XML_Tree_Node for that element is sent to the fromNode($node) method of the XML_SvgToPdf_* classes (most of which are handled by the base class). These just flatten out the attributes into object properties, copy the children, and parse the style tag.

To convert it to PDF, you then just implement a toPDF method in each of the classes, and send it the $pdf (from FPDF), and $data - what data you want to replace the {xxxx} tags with.

To get the 'dynamic looping' eg. for the mailing list I needed 3 columns and 7 rows, I created
  • A text item on the SVG with
    dynamic=name_of_var_to_use
    rows=3
    cols=7
  • A rectangle, that had the sodipodi flag (not printable - under item properties)
  • make these, and the text {xxxx} into a group.
the parser then checked every group looking for xxx=yyy text, if it found it, it was flagged as dynamic.

The result has been so effective, I have started replacing all the old 'text' based reports with stylish PDF's.


Mentioned By:
www.fpdf.org : SVG template engine (3113 referals)
google.com : php svg (353 referals)
www.fpdf.org : Moteur de template SVG (295 referals)
google.com : fpdf svg (218 referals)
google.com : (184 referals)
google.com : php svg to pdf (155 referals)
google.com : php svg pdf (131 referals)
google.com : svg to pdf php (129 referals)
marc.info : '[PEAR] Re: XML to pdf conversion, xhtml to pdf conversion' - MARC (102 referals)
google.com : php svg parser (96 referals)
google.com : april (83 referals)
marc.theaimsgroup.com : MARC: msg '[PEAR] Re: XML to pdf conversion, xhtml to pdf conversion' (81 referals)
google.com : php svg library (74 referals)
google.com : december (66 referals)
google.com : php pdf svg (61 referals)
google.com : svg fpdf (58 referals)
google.com : svg php (58 referals)
google.com : svg pdf php (56 referals)
google.com : php and svg (50 referals)
google.com : SEXY.PDF (48 referals)

Comments

Do you have any example svg file?
#0 - Luguber123 ( Link) on 2004-01-13 19:18:38 Delete Comment
You can find an example here:

http://www.fpdf.org/?go=script&id=43
#1 - Olivier ( Link) on 2004-03-15 03:59:32 Delete Comment
I'm not familiar with PEAR. Could you either direct me to some good resources or explain where to install the packages necessary to run SVGToPDF?
#2 - Sean ( Link) on 2004-08-05 03:04:05 Delete Comment
#3 - Olivier ( Link) on 2004-08-05 03:50:17 Delete Comment
InkScape (http://inkscape.sourceforge.net) may be a better idea than sodipodi, It appears to preserve attributes, so rather than the current kludgy way of writing block names etc. it should be possible to use id="{thename}" and svgtopdf:columns="4" as attributes of the g tag.
#4 - Alan ( Link) on 2004-08-15 22:14:58 Delete Comment
SVGTOPDF con imagenes
primero que nada dejame darte las gracias por tal programa la cual me fue de mucha ayuda para generar etiquetas
para un empresa de courier, ahora ya que quiero es hacerlo con imagenes
pero en la direccion

http://www.fpdf.org/?go=script&id=43

dice que no soporta imgenes

Nota: las im�genes y las curvas no se apoyan.
Usted puede encontrar m�s detalles aqu� .


Necesito saber como hacerlo ya que lo que quiero hacer es mostrar en vez de etiquetas codigos de barras las cuales los genero como una imagen con php y genbarcode.
#5 - victor ( Link) on 2006-03-03 07:40:38 Delete Comment
Doesn't work with Inkscape
Hi,

Nice program. Unfortunately it doesn't seem to work with Inkskape. I opened up the ex.svg file, removed a single character and put it back again (ie. it should be the same), but upon saving and re-generating the PDF it comes up with an "OOPS unknown object?" and dumps some output. See here for the output: http://paste.ubuntu.com/47963/

Alan, I'm not sure what you mean when you mention about it should be possible to use svgtopdf:columns="4", etc. Do you mean it would be possible with modifications to the SvgToPdf program? Or should it already perhaps be possible?

Cheers.

EDIT: Fixed link to my homepage, feel free to delete the other comment, ta :)
#6 - Carey ( Link) on 2008-09-18 13:38:10 Delete Comment

Add Your Comment

Follow us on