Skip Navigation

[Résolu] PDF generation from the site bis

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

This topic contains 17 réponses, has 2 voix.

Last updated by Adriano Il y a 7 années et 2 mois.

Assigned support staff: Adriano.

Auteur
Publications
#230232

Pat

Hello,

Following my last ticket (https://toolset.com/forums/topic/pdf-generation-from-the-site/), I have found a librairy able to make the translation HTML to PDF.
(hidden link).
In order to make it work, I need to place this kind of code :

<?php
$content = "
<page>
<h1>Exemple d'utilisation</h1>
<br>
Ceci est un <b>exemple d'utilisation</b>
de <a href='hidden link;.<br>
</page>";

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

My issue here is that I need to retreive the content of my content template in a variable ($content).
How can I do this with Toolset content template?
Regards
Pat

#230249

Dear Pat,

I think you are looking for the filter "wpv_filter_content_template_output": https://toolset.com/documentation/user-guides/views-filters/wpv_filter_content_template_output/

#230763

Pat

Hello Adriano,
Thanks for your feedback. Does that work also for a specific postype ?

What's the best way to create a link to save the pdf file (I need to have a button in the content template page in order to give the opportunity to save the file). Do I need to create a shortcode for this and if yes, can you help with the code?

Regards
Pat

#230812

Dear Pat,

As we talked here sometime ago, Toolset doesn't provide such feature: https://toolset.com/forums/topic/pdf-generation-from-the-site/

Of course, using wpv_filter_content_template_output you can check every post field, like post type:

if(get_post_type(get_the_ID()) == 'post_type_slug'){
     //your code goes here
}
#233136

Pat

Hello Adriano,
In fact, I have found a library that enables this feature. The issue is that all command lines are in php and I don't know what's the best way to link this php code with the button I have in my content template.
Do you have an idea how to do this in a simple way?
Regards
Pat

#233345

Dear Pat,

The only way is using custom shortcodes. Content Templates are fully compatible with it: http://codex.wordpress.org/Shortcode_API

Please let me know if you are satisfied with my answer and if I can help you with any other related question.

#233382

Pat

Hello Adriano,
How to include php in a shortcode ?
Here's the lines I need to integrate :

<?php
$content = "
My page content";

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

Regards
Pat

#233519

Dear Pat,

I've just converted this code in a shortcode. You should insert it in functions.php:

function pdf_func( $atts ){
   content = "
   My page content";
   require_once(dirname(__FILE__).'/html2pdf/html2pdf.class.php');
   $html2pdf = new HTML2PDF('P','A4','fr');
   $html2pdf->WriteHTML($content);
   $html2pdf->Output('exemple.pdf');
}
add_shortcode( 'pdf-generate', 'pdf_func' );

Not sure what this code should do, but you can call this shortcode like this: [pdf-generate]

Please let me know if you are satisfied with my answer and if I can help you with any other related question.

#233654

Pat

Hello Adriano,

Seem good. Thanks.
Last point now, If I need to replace:

content = " My page content";

by what's is currently in my view content template, how can I get this?
Regards
Pat

#233732

Dear Pat,

So you will need to change the shortcode with:

function pdf_func( $atts, $content = null ){
   require_once(dirname(__FILE__).'/html2pdf/html2pdf.class.php');
   $html2pdf = new HTML2PDF('P','A4','fr');
   $html2pdf->WriteHTML($content);
   $html2pdf->Output('exemple.pdf');
}
add_shortcode( 'pdf-generate', 'pdf_func' );

Then you should call this shortcode like this:

[pdf-generate]
Here goes the Content Template's content
[/pdf-generate]

The shortcode will add in the variable $content the content inside inside of the shortcode.

Please let me know if you are satisfied with my answer and if I can help you with any other related question.

#234465

Pat

Hello Adriano,

Thanks for your support.
Now, I would like to launch the action with a button. I'm not sure how to integrate your shortcode inside a button?

Regards
Pat

#234503

Dear Pat,

I can't see a way to do this. That's not possible to make a button to trigger some hook, because all shortcodes are loaded in functions.php before of everything.

#234504

Pat

Hello Adriano,

What about something like this :

<button onclick="pdf_func()">Try It</button>

Regards
Pat

#234657

Dear Pat,

You can't execute PHP functions inside of the onclick, just JS ones.

#234987

Pat

Hello Adriano,

In this case, is it possible to include the PHP in the content template via a <script></script> for example instead of placing the code in the function.php file ?
Regards
Pat