How to create a fpdf wordpress plugin

2019-08-08 10:15发布

问题:

I'm familiar with creating wordpress plugins. I tried hard to make fpdf plugin to create pdf file from php contents, but IT didn't work

I now that : require_once("fpdf.php"); should comes at the top of my file, and I think that is my main problem

below is how to create a simple pdf file from php script using fpdf:

require('fpdf.php');
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(40,10,'Hello World!');
$pdf->Output();

What I should do to make it work?

回答1:

The question is, how do you convert your html to pdf output? To get your PDF to look like your webpage, that is, with all the html+css, I don't believe this library will do it.

If you just want to get the post_content from a post, and put it into a pdf, you could do this:

To create a plugin:

You'll need to add a folder to /wp-content/plugins/ and create your plugin file in that folder:

/wp-content/plugins/fpdf/fpdf.php

/*
Plugin Name: fpdf
Description: Post to PDF
*/

// Here's where you'll write your code.
$post = get_post( $post_id );

require('fpdf.php');
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(40,10,$post->post_content);
$pdf->Output();

This would output a pdf on every page load, after you activate the plugin, which is not likely what you're looking for...



回答2:

If you are sure the problem in require('fpdf.php'); then use full file path as follows

define( 'MYPLUGINNAME_PATH', plugin_dir_path( __FILE__ ) );
require MYPLUGINNAME_PATH . 'fpdf.php';