Question

I was just given the order of "Make a button that when clicked, creates a pdf out of the page the user is currently seeing. This is a rug company and they want a PDF button that when clicked, will create a pdf with page title/rug/rug desciption etc.

To be clear, this is all in Wordpress with dynamic data. I havent found a plugin that will do what i want so, im trying manually.

Overall, this is what I've tried:

The way ive gone about doing this is as follows: - For one I am using DOMPDF. With this below, it creates the pdf. This lives in a file called "createpdf.php"

$htmlToPdf = //MY HTML HERE\\ ;

$dompdf = new DOMPDF(); 
$dompdf->load_html( $htmlToPdf );
$dompdf->render();
$dompdf->stream("sample.pdf");

The part that reads $htmlToPdf = //MY HTML HERE\\ ; is where my HTML will go.

Now since this is WordPress, i am generating the page content via simple WP loop like so:

<!-- post starts here -->
        <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
        <div class="post">
            <h2 class="post-title"><?php the_title(); ?></h2>
            <span class="meta"><?php _e('Published'); ?> <?php the_time('F j, Y'); ?> <?php _e('in'); ?> <?php the_category(', ') ?></span>
            <div class="entry">
                <?php the_content(); ?>
            </div><!-- end entry -->
        </div><!-- end post -->
        <?php endwhile; endif;?>
<!-- post ends here -->

Now, what i want to do is take the parts that matter from the page which is the content wrapper(loop included) and stick it into a PHP variable called(as shown above) $htmlToPdf = //MY HTML HERE\\ ; Then echo out that variable onto the page (single.php). This way, the page shows like normal to a user BUT with the added benefit that that the content is now in a variable so that when a user clicks the "create pdf button", it will take that pages current content and send it to "createpdf.php" via something like button

etc. Then i can do:

$htmlToPdf = $_GET['maketopdf']; 

and push $htmlToPdf fowards towards the pdf creation.

im getting so confused lol....

So i put the content in a variable like so:

 <?php 
$htmlToPdf = '
<!-- post starts here -->

        <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
        <div class="post">
            <h2 class="post-title"><?php the_title(); ?></h2>

            <div class="entry">
                <?php the_content(); ?>
            </div><!-- end entry -->
        </div><!-- end post -->
        <?php endwhile; endif;?>
<!-- post ends here --> ';

echo $htmlToPdf;
?>

Problem now is that, no i don't get on page errors but nothing shows on the content column either. The page loads fine but where the content is supposed to be, it comes out empty.

How can i get this to show normally on single.php? whilst still using the methods im trying above.

Hope this isn't too confusing. Any help is GREATLY appreciated. Thanks in advanced.

Was it helpful?

Solution

There's always this ugly workaround ...

<?php
$htmlToPdf = '';
if (have_posts()){
    while (have_posts()){
        the_post(); 
        $htmlToPdf .= '<div class="post">';
        $htmlToPdf .= '<h2 class="post-title">'.the_title().'</h2>';
        $htmlToPdf .= '<div class="entry">';
        $htmlToPdf .= the_content();
        $htmlToPdf .= '</div><!-- end entry -->';
        $htmlToPdf .= '</div><!-- end post -->';
    }
}
echo $htmlToPdf;
?>

OTHER TIPS

I'll use JS/jQuery to get the current page source and send it to your php script (the one who generates PDF) :

$('.yourButton').click(function(e){
   e.preventDefault();
   var dataToSend = document.documentElement.outerHTML;
   $.post('/path/to/your/DOMPDFfile.php', {data: dataToSend});
   window.open('path/to/the/generated.pdf');
});

Then your DOMPDF file :

$htmlToPdf = $_POST['data']; ;

$dompdf = new DOMPDF(); 
$dompdf->load_html( $htmlToPdf );
$dompdf->render();
$dompdf->stream("sample.pdf");

Note that it use a jQuery AJAX method.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top