Wordpress Post to pdf
<?php
class post_to_pdf {
public $content;
public $title;
public $down;
function __construct() {
}
//retrieve data
public function get_post_contents($down="yes", $post_id) {
echo "started";
global $post;
global $wpdb;
$this->down = $down;
$str = get_post_field('post_content', $post_id);
$str = preg_replace('/\[crea_pdf_btn.*?\]/', '', $str);
$this->content = do_shortcode($str);
$this->title = get_the_title($post_id);
$this->create_pdf();
echo $this->title." - ".$this->content;
}
//create PDF
function create_pdf() {
ob_clean();
include("mpdf/mpdf.php");
$mpdf = new mPDF();
$mpdf->SetDisplayMode('fullpage');
//$stylesheet = file_get_contents('mpdf/styleCorsiPdf.css');
$mpdf->WriteHTML($this->content);
if ($this->down == "no") {
$mpdf->Output();
} elseif ($this->down == "yes") {
$pdfNameFinal = $this->title . ".pdf";
$mpdf->Output($pdfNameFinal, 'D');
}
}
}
$ptpdf = new post_to_pdf();
// BUTTON SHORTCODE
function crea_pdf_btn($atts) {
$shortcode_atts = (shortcode_atts(array(
'download' => "yes",
), $atts));
$down = $shortcode_atts[ 'download' ];
$pdf_btn = '<div>
<form action="'.get_theme_root_uri().'/woptimizr/post_to_pdf/index.php" method="post" id="ptpdf_form" >
<input type="hidden" name="ptpdf_down" value="'.$down.'">
<input type="hidden" name="ptpdf_id" value="'.get_the_ID().'">
<div class="btn btn-sm btn-danger download_pdf">download PDF <i class="fa fa-file-pdf-o mar-lft"></i></div>
</form>
</div>';
return $pdf_btn;
}
add_shortcode('crea_pdf_btn','crea_pdf_btn');
// ENQUEUE JS
wp_enqueue_script( 'post-to-pdf-js', get_theme_root_uri().'/woptimizr/post_to_pdf/post_to_pdf.js' );
?>
<?php
require_once( '../../../../wp-load.php' );
if (isset($_POST['ptpdf_down']) && isset($_POST['ptpdf_id'])) {
$ptpdf_down = $_POST['ptpdf_down'];
$ptpdf_id = $_POST['ptpdf_id'];
$ptpdf->get_post_contents($ptpdf_down,$ptpdf_id);
} else {
header('Location: ' . $_SERVER['HTTP_REFERER']);
}
?>