How to create a fpdf wordpress plugin

随声附和 提交于 2020-01-05 10:07:48

问题


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';


来源:https://stackoverflow.com/questions/17948418/how-to-create-a-fpdf-wordpress-plugin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!