Problems, need help? Have a tip or advice? Post it here.
8 posts Page 1 of 1
Hi KK,
I'm using html2pdf to create PDF documents from HTML files (hence the name). This time I tried with couch but it doesn't work. Did I miss something?

Original code: works perfectly
<?php
$content = "
<page>
<h1>Exemple d'utilisation</h1>
<br>
Ceci est un <b>exemple d'utilisation</b>
de <a href='http://html2pdf.fr/'>HTML2PDF</a>.<br>
</page>";

require_once(dirname(__FILE__).'/html2pdf/html2pdf.class.php');
$html2pdf = new HTML2PDF('P','A4','fr');
$html2pdf->WriteHTML($content);
$html2pdf->Output('exemple.pdf');
?>

Couchified version: blank page
<?php require_once( 'doggy/cms.php' ); ?>
<cms:php>
$content = "
<page>
<h1>Exemple d'utilisation</h1>
<br>
Ceci est un <b>exemple d'utilisation</b>
de <a href='http://html2pdf.fr/'>HTML2PDF</a>.<br>
</page>";

require_once(dirname(__FILE__).'/html2pdf/html2pdf.class.php');
$html2pdf = new HTML2PDF('P','A4','fr');
$html2pdf->WriteHTML($content);
$html2pdf->Output('exemple.pdf');
</cms:php>
<?php COUCH::invoke(); ?>
Hi Paolo,

HTML2PDF library expects to be handled the final HTML output of the page to do its work.
To make it work with Couch, we'll have to hand it over the HTML generated by Couch (as opposed to calling it from within Couch, which won't work).

To do so, we can do the following -
Suppose this is a skeletal Couch template -
Code: Select all
<?php require_once( 'couch/cms.php' ); ?>

<h1>PDF Test</h1>
.. original content ..

<?php COUCH::invoke(); ?>

Wrap the above with code that calls HTML2PDF like this
Code: Select all
<?php ob_start(); ?>
<?php require_once( 'couch/cms.php' ); ?>

<h1>PDF Test</h1>
.. original content ..


<?php COUCH::invoke(); ?>
<?php
    $content = ob_get_clean();

    // convert to PDF
    require_once(K_SITE_DIR. 'html2pdf/html2pdf.class.php');
    try
    {
        $html2pdf = new HTML2PDF('P', 'A4', 'fr');
        $html2pdf->writeHTML($content, isset($_GET['vuehtml']));
        $html2pdf->Output('exemple13.pdf');
    }
    catch(HTML2PDF_exception $e) {
        echo $e;
        exit;
    }
?>

As you can see, now the first and the last executing PHP statements are those of html2pdf instead of Couch. Also please note that the code above expects the 'html2pdf' folder to be situated inside the site's root (i.e. Couch's parent folder).

Mind you, in my testing I found html2pdf to be extremely finicky about the HTML it is given (needs to be 100% validated).

Hope this helps.
Thanks KK, YOU'RE THE MAN !

you're right about html2pdf, it requires 100% validation, which is a bit frustrating sometime.
Plus there's many basics it doesn't accept like the <caption> on tables, etc.
But still, it's ok.

Cheers,
Paolo E.
You are welcome, Paolo :)
Glad to be of help.
sorry, dumb question. I tried to erase it, but it doesn't seem possible.
Cheers
Sorry, couldn't get it. Erase what?
Hi again KK,

Ok, here I come again, I gave up.

Since I can't call the HTML within couch for the HTML2PDF and use the template with the regulars if k_is_list/if k_is_page, I opened a session on my main page form.php and transferred the values of my variable (nom, prenom, date_n, etc.) to a new page where I use the variables (pdf.php). simple - (I made it available in kfunctions.php).

So
<cms:set_session name='nom' value="<cms:show nom />" /> (in form.php)
and a <cms:get_session nom /> (in pdf.php)
does the trick.

But, since I have 50 variables for the databound form, I didn't want to code all variables one by one, so I wondered if there was a way to glob(); the variables of the template in a string...

Then I realized it wasn't necessary since I could use the <cms:page>, but I can't make it work, and I dont understand why.

In form.php :

<cms:set_session name='pdf' value="<cms:show k_page_title />" />

and in pdf.php I call :

<cms:get_session 'pdf' />
<cms:pages masterpage='form.php' page_name='pdf'>
<cms:show nom />
[...]
</cms:pages>


but nothing works... except <cms:show pdf />which give me the page number aacfd35b50b8e62ad12462e1e4f64fa1 that I asked for.

I just don't get it.
Ok, nobody moves!

sometime it's enough to get his head out of it and simply write it down. I get it:
<cms:pages masterpage='form.php' page_name="<cms:get_session 'pdf' />" >

Cool !

PDF, here I come...

Cheers KK,
Paolo E.

PS: it's bluffing how simple things can be easy to miss sometime...
8 posts Page 1 of 1