I want to be able to export a html page to PDF. I downloaded TCPDF (mainly because it allows me save High resolution images and shrinks the filesize.) The TCPDF folder is in my theme folder.

If a user clicks on a link in a certain page the current page should be saved as a PDF.

The link contains an element to pass the page ID.

<a href="<?php get_bloginfo('template_directory')?>/tcpdf/export/export_pdf.php?id=<?php echo $id; ?>">Click here to PDF this page</a>

In the export_pdf.php file I used $id = $_GET['id'] to retrieve the ID from the URL. With the ID I want to get_post_meta($id); like that but it gives me the following error:

Fatal error: Call to undefined function get_post_meta() in...

I use get_post_meta to retrieve data from outside the loop but I think I'm wrong :-)

This is the code I use in TCPDF to write HTML:

global $post;
$id = $_GET['id'];
$review = get_post_meta($id);
$thispost = get_post( $review );
$content = $thispost->post_content;

// output the HTML content
$pdf->writeHTML($content, true, false, true, false, '');

I hope somebody can help me with this.

  • "I found a solution but I've got a new problem". Since this is a question / answer format and not a traditional forum it's best to ask one question at a time. If you have an answer to your solution it should be posted in the answer section below so that it's indexed by the website global search ( comments are not indexed in search ). If you have more questions it's best to create an entirely new question especially since it appears that the new issue is separate from the post title and bulk of the main content.
    – Howdy_McGee
    Jan 13, 2015 at 16:27
  • 2
    @Interactive No need to flag your own comments. That isn't a good idea as it ads to flag count against you :) You can clean up your comment by ticking the little x at the end of your comment yourself :)
    – kaiser
    Jan 13, 2015 at 16:56

1 Answer 1


I've got it fixed:
I needed to add the wp-load file that contains all the WP-functions

global $post;
$id = $_GET['id'];
$content_post = get_post($id);
$content = $content_post->post_content;
$content = apply_filters('the_content', $content);
$content = str_replace(']]>', ']]&gt;', $content);

Now the content loads in the PDF

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.