admin管理员组文章数量:1123889
How to generate PDF by using the values from database without using plugin in WordPress.
For example, I have a form and will enter the Name. Then it needs to fetch the data from DB based on the entered Name and need to generate the PDF dynamically.
How to generate PDF by using the values from database without using plugin in WordPress.
For example, I have a form and will enter the Name. Then it needs to fetch the data from DB based on the entered Name and need to generate the PDF dynamically.
Share Improve this question edited Mar 22, 2024 at 6:54 Manikandan asked Mar 21, 2024 at 11:28 ManikandanManikandan 11 bronze badge 2- 1 Not strictly speaking from the database, but I've used these tutorials to generate PDFs using custom fields from posts on my WordPress site before: Tutorial 1, Tutorial 2, Tutorial 3. You'd just need to fill in the missing step of retrieving data from the database. – YourManDan Commented Mar 21, 2024 at 14:36
- 1 note that you can ask how to do it, but you can't ask for recommendations of tutorials for it, asking for recommendations here is offtopic and you need to be able to mark an answer as the correct answer, not the most useful link – Tom J Nowell ♦ Commented Mar 21, 2024 at 15:20
1 Answer
Reset to default 1Using the FPDF library (found at http://www.fpdf.org) you generate PDFs on the fly using any data from your WP site.
function wpse60844_generate_pdf( $post ) {
/* Define paths for where the PDF will be permanently or temporarily saved */
define( 'FPDF_PATH', plugin_dir_path( __DIR__ ) . '/fpdf/' );
define( 'SAVE_PATH', plugin_dir_path( __DIR__ ) . 'fpdf/pdfs/' );
$pdf_dir = plugin_dir_path( __DIR__ ) . 'fpdf/pdfs/';
/* If the directory doesn't already exist, generate it */
if( !file_exists ( $pdf_dir ) ) :
wp_mkdir_p( $pdf_dir );
endif;
/* Bring in the FPDF files so this all works */
require( FPDF_PATH .'fpdf.php' );
require( FPDF_PATH .'writeHTML.php' );
/* Start retrieving your data from the WP DB, whether it's post_meta or some other data, no difference, just get your data into variables */
$yourdata = get_post_meta( $post->ID, 'meta_key', true );
$moredata = get_post_meta( $post->ID, 'meta_key', true );
/* Now you begin to generate the HTML that will be converted to a PDF
* Careful as some standard HTML won't work, like `<strong>` or `<em>`, use `<b>` or `<i>` */
$html = '<b>Some Label: ' . $yourdata . '</b><br><br>';
$html .= '<b>Some other Label: ' . $moredata . ' </b><br>';
/* Keep going until you have all the content and layout you need */
/* generating the PDF itself */
$pdf = new hFPDF( 'P', 'mm', 'A4' );
$pdf->AddPage();
$pdf->SetFont( 'Arial', '', 12 );
/* this is where the actual HTML you built above gets added as PDF. */
$pdf->WriteHTML( utf8_decode( $html ) );
$pdf->Output( SAVE_PATH . 'whatever-your-filename-is.pdf', 'F' );
global $pdf_filename;
$pdf_filename = SAVE_PATH . 'whatever-your-filename-is.pdf';
/* Next you either have to write a function to mail this to your self using `wp_mail()`
* Or send over the download path via AJAX and then download the PDF
* To keep things clean you should also probably delet the PDF after it has been downloaded/sent
* The rest is really up to you. */
}
add_action( 'the_action_that_triggers_pdf_send_or_download', 'wpse60844_generate_pdf' );
There's a lot of detail work involved and the odd character here or there can break the whole process. So be careful and I would honestly try to first output a single line PDF and then line by line add in the extra data, as that'll make it easier to help figure out where an error popped up.
本文标签: mysqlHow to generate PDF dynamically based on values from the database in WordPress
版权声明:本文标题:mysql - How to generate PDF dynamically based on values from the database in WordPress 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736604275a1945285.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论