PDF Generation with Symfony2 – Part II: Zend Option

To start this post we must acknowledge the existence of a Zend library to fill in pdfs here and also the fact that there were some people who had used it before even if it was as far as I know only in the 1.x version like here.

The integration between Symfony2 and Zend2 is rather simple since both support namespaces the only thing left is (adapted from here):

git submodule add git://github.com/zendframework/zf2.git vendor/Zend

And register the namespace:

 'Zend'                => __DIR__.'/../vendor/Zend/library',

And the demo code:

$zf2pdf = new \Zend\Pdf\PdfDocument(__DIR__.'/../pathtoyourfile/test.pdf', null, true);
$page = $zf2pdf->pages[0];
$style = new \Zend\Pdf\Style();
$style->setFont(\Zend\Pdf\Font::fontWithName(\Zend\Pdf\Font::FONT_HELVETICA),12);
$style->setFontSize(12);
$page->setFont($style->getFont(), $style->getFontSize());
$page->drawText('Hello world!', 72, 720);
$zf2pdf->save(__DIR__.'/../pathtoyourfile/testout.pdf');

Here we just used the Zend framework classes to work it out without documentation like it used to be in Symfony2. Please help us help you and write more blogs like this by donating on the top right corner of the site.

Virtualbox Again: NDIS5 drivers for Windows XP

I just ran into a problem again trying to emulate windowxp images from microsoft.
Basically download the driver:
“NDIS5 Driver for Microsoft Windows Server 2003, Windows XP, Windows 2000, Windows ME and Windows 98” from amd.com website. Yes it will be there it is very hard to find but basically go to driver Catalysts then to drivers then network and refine your search until you get it. Then extract the file it is called V4.51.zip then:

The link is currently:
http://support.amd.com/us/Pages/dynamicDetails.aspx?ListID=b7383048-1ac6-4dd5-9f0e-087bb719e48e&ItemID=476&lang=us
http://support.amd.com/us/Embedded_TechDownloads/V4.51.zip

genisoimage -o driver2.iso V4.51/

And mount it as CD from your opened vbox running instance of windows xp. And install the pcnet drivers. That is it you now have network.

This guy helped me a bit here.

PDF Generation with Symfony2 – Part I: SnappyBundle Experience

Update: I apologize for the strong wording I had posted earlier. Basically it is out of frustration. However I do think SnappyBundle does a great job. It is just that I was looking to do something different while manipulating pdfs.

So after I fought with it for long hours and posted an issue to the repo:

Broken get contents
============
Warning: file_get_contents(/tmp/knplabs_snappyf35hGd.pdf): failed to open stream: No such file or directory in /home/cordoval/sites-2/x/vendor/knplabs/snappy/src/Knplabs/Snappy/Media.php line 87
your syntax is jeopardizing the whole thing too, tried several variants but the same trouble
'Content-Disposition' => 'attachment; filename="file.pdf',
see the missing quotes?
grrr so frustrating.

I realized that it had to be another solution.

Symfony2 Multiple Forms: Different From Embedded Forms

One thing to have clear in mind is that embedded forms is different from having multiple forms in one page.
With embedded forms single or collection the problem that is solved is always submission of a single form composed of different entities.
With multiple forms the problem that is solved is submission of multiple forms persisting different sets of entities and properties depending to which form is submitted.

So here is the format that does the trick for multiple forms. I makes use of the createNamedBuilder method which gets the type forms for an entity of class A. It also receives as parameter the name of the form we would like to label it. So we define two forms in the code below, each including two different properties of different entity, although they could be the same. Then we make sure we receive the POSTed information writing a conditional from where we can handle each form separate and persist each corresponding entity discriminating according to which form was submitted. Lastly we see that we pass to the twig engine two variables for two separate forms.

public function multiformAction()
{
    $form1 = $this->get('form.factory')->createNamedBuilder($formTypeA, 'form1name')
        ->add('foo', 'text')
        ->getForm();
 
    $form2 = $this->get('form.factory')->createNamedBuilder($formTypeB, 'form2name')
        ->add('bar', 'text')
        ->getForm();
 
    if('POST' === $request->getMethod()) {
 
        if ($request->request->has('form1name') {
            // handle the first form
        }
 
        if ($request->request->has('form2name') {
            // handle the second form
        }
    }
 
    return array(
        'form1' => $form1->createView(),
        'form2' => $form2->createView()
    );  
}

Notice also that the $request variable is the sf2 request object. This object has a property called request too. The type of this property is ParameterBag which in turn has a method called has which checks for the given key within the object.

Within the twig templates obviously we are going to call separately the code for each form like:

{{ form_row(form1.foo) }}
...
{{ form_row(form2.bar) }}

That is it. Thanks for donating!