Generate pdf from custom post type with custom meta fields? - Wordpress Solution
[Solved] - Generate pdf from custom post type with custom meta fields? - Wordpress Solution
Try following codes
I use a custom post type (ansatte - meaning employees) to register/show the employees of a company (or actually, several daughter-companies of one company), and the single-ansatte.php shows the CV, contact-info and so on, for each individual employee. I think I have tried all the different plugins available, but some doesn't seem to work, and the ones I have found that otherwise works, doesn't include the meta fields. Using some outside service to generate the pdf is not an option. Then I'll rather make the pdf's by hand and use a meta field for the link. Except from outside services and branded pdf's, almost any solution is acceptable. As you see, I also have an option to download a vcard with the mail/phone++ of the employee, [[LINK href="http://www.wpquestions.com/question/show/id/2575"]]solved very nice, simple & elegant by John Cotton here[[/LINK]]. The ideal solution would be something similar to johns solution, where I can include the meta fields I want to and I can use some html and css to get a nice layout on the pdf. I will need an example of working code since my php-skills really suck :) Here is the code for the single-ansatte.php and the custom post type including the meta fields. Please ask if there is any other info you need. If I get a good solution quickly, I'll also add a bonus. single-ansatte.php

 * Mal for ansattes CV.
 * @package WordPress

// er det vcard som skal lastes ned?
if( isset( $_GET['vcard'] ) ) {

	global $post;


	$custom = get_post_custom($post->ID);

	header('Content-type: text/x-vcard; charset=utf-8');

	header('Content-Disposition: attachment; filename="vcard.vcf"');

	echo 'BEGIN:VCARD'. "\n";

	echo 'VERSION:3.0'. "\n";
	echo 'N:'.$custom["etternavn"][0] .';'.$custom["fornavn"][0]. "\n";
	echo 'FN:'.$custom["fornavn"][0] .' '.$custom["etternavn"][0]. "\n";
	echo 'ORG:Industrifinans'. "\n";
	echo 'TITLE:' .$custom["stilling"][0]. "\n";

	echo 'TEL;type=CELL;type=pref:'.$custom["tlf"][0]. "\n";

	echo 'EMAIL;PREF;INTERNET:'.$custom["mail"][0]. "\n";
	echo 'X-ABShowAs:COMPANY'. "\n";

/// etc for each field

	echo 'END:VCARD';



// vcard slutt

get_header(); ?>

		<div id="container">
			<div id="content" role="main">
				<div id="firmaliste">
				<div id="ansattliste">

<?php $custom = get_post_custom($post->ID);  
$etternavn = $custom["etternavn"][0];  
$fodselsar = $custom["fodselsar"][0];  
$tlf = $custom["tlf"][0];  
$mail = $custom["mail"][0];  
$about = $custom["about"][0];  
$fornavn = $custom["fornavn"][0];?>

<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>	
				<div id="cvpage" <?php post_class(); ?>>
					<h1 class="entry-title"><?php the_title(); ?> (<?php echo $fodselsar; ?>)</h1>
					<div class="entry-content cv">
						<p><?php echo $about; ?></p>
						<?php the_content(); ?>
					</div><!-- .entry-content -->

					<div class="entry-utility">
						<?php edit_post_link( __( 'Edit', 'twentyten' ), '<span class="edit-link">', '</span>' ); ?>
					</div><!-- .entry-utility -->
				</div><!-- #post-## -->
				<div id="ansattinfo">
					$thumb = get_the_post_thumbnail($post->ID);
					$pattern= "/(?<=src=['|\"])[^'|\"]*?(?=['|\"])/i";
					preg_match($pattern, $thumb, $thePath);
					$theSrc = $thePath[0]; ?>
if ( has_post_thumbnail() ) {
	// the current post has a thumbnail
	echo '<p><a href="'."$theSrc".'">Last ned høyoppløst bilde</a></p>';

} else {
	// the current post lacks a thumbnail
					<div id="ansattinfo-tekst">
					<p><strong>Tlf:</strong> <?php echo $tlf; ?></p>
					<p><?php if ( function_exists( 'joliprint_show_the_button' ) ) echo joliprint_show_the_button(); ?></p>
					<p><strong>Epost:</strong> <a href="mailto:<?php echo $mail; ?>"><?php echo $mail; ?></a></p>
					<p><strong><a href="<?php the_permalink(); ?>?vcard=1">Lagre som kontakt (vcard)</a></strong></p>
<?php endwhile; // end of the loop. ?>

			</div><!-- #content -->
		</div><!-- #container -->

<?php get_footer(); ?>
The code for the custom post type and the meta fields:

add_action('init', 'ansatte_register');

function ansatte_register() {
	$labels = array(
		'name' => _x('Ansatte', 'post type general name'),
		'singular_name' => _x('Ansatte', 'post type singular name'),
		'add_new' => _x('Legg til', 'ansatt'),
		'add_new_item' => __('Legg til ny ansatt'),
		'edit_item' => __('Rediger ansatt.'),
		'new_item' => __('Ny ansatt.'),
		'view_item' => __('Vis ansatt'),
		'search_items' => __('Søk i ansatte'),
		'not_found' =>  __('Ingenting funnet'),
		'not_found_in_trash' => __('Ingenting funnet i søppel'),
		'parent_item_colon' => ''
	$args = array(
		'labels' => $labels,
		'public' => true,
		'publicly_queryable' => true,
		'show_ui' => true,
		'query_var' => true,
		'menu_icon' => get_stylesheet_directory_uri() . '/article16.png',
		'rewrite' => true,
		'capability_type' => 'post',
		'hierarchical' => false,
		'menu_position' => null,
		'supports' => array('thumbnail','title','editor')
	register_post_type( 'ansatte' , $args );
function ansatte_meta() {
	global $post;
	$custom = get_post_custom($post->ID);
	$fornavn = $custom["fornavn"][0];
	$etternavn = $custom["etternavn"][0];
	$fodselsar = $custom["fodselsar"][0];
	$about = $custom["about"][0];
	$stilling = $custom["stilling"][0];
	$mail = $custom["mail"][0];
	$tlf = $custom["tlf"][0];
	$cvlink = $custom["cvlink"][0]; ?>
	<p><label>Fornavn:</label><br />
	<input type="text" name="fornavn" value="<?php echo $fornavn; ?>"></input></p>
	<p><label>Etternavn:</label><br />
	<input type="text" cols="50" rows="1" name="etternavn" value="<?php echo $etternavn; ?>"></input></p>
	<p><label>Fødselsår:</label><br />
	<input type="text" cols="50" rows="1" name="fodselsar" value="<?php echo $fodselsar; ?>"></input></p>
	<p><label>Kort tekst om ansatt:</label><br />
	<textarea cols="50" rows="5" name="about"><?php echo $about; ?></textarea></p>
	<p><label>Stillingstittel:</label><br />
	<input type="text" name="stilling" value="<?php echo $stilling; ?>"></input></p>
	<p><label>Telefonnr:</label><br />
	<input type="text" cols="50" rows="1" name="tlf" value="<?php echo $tlf; ?>"></input></p>
	<p><label>E-postadresse:</label><br />
	<input type="text"  cols="50" rows="1" name="mail" value="<?php echo $mail; ?>"></input></p>
	<p><label>Link til CV:</label><br />
	<input type="text" cols="50" rows="1" name="cvlink" value="<?php echo $cvlink; ?>"></input></p>

register_taxonomy("firma", array("ansatte"), array("hierarchical" => true, "label" => "Firma", "singular_label" => "Firma", "rewrite" => true));

Solution - 1

How about using [[LINK href="https://code.google.com/p/dompdf/"]]dompdf[[/LINK]] to convert your html to pdf? It has some caveats and it's a bit overhelming with large files, but i think it's perfect for the task you're asking

Solution - 2

Hi, If you still cannot get your pdf library to work. You may want to try using this FPDF library. http://www.fpdf.org/ You can download the whole library zip file that includes the demos. Just put the whole folder within your htdocs and navigate to the examples. All demos work out of the box. They got well documented manual online too. You can follow the demo codes to familiarize with how it works, before applying to your codes. Library is written in Object Oriented Programming. No additional PHP extension required. Thanks. Denzel

Date: 2022-10-21 05:12:31
Category: Wordpress
More for You