Create PDf on woocommerce_thankyou page
-
Hello,
I want to create a pdf file from an html file and email it to the user by the woocommerce_thankyou hook.Here is some of my code in function.php:
add_action('woocommerce_thankyou', 'send_thank_you_email', 1, 1);
function send_thank_you_email($order_id)
{
$order = wc_get_order($order_id);
$order_email = $order->get_billing_email();
$first_name = $order->get_billing_first_name();
$last_name = $order->get_billing_last_name();
$_req_res = _send_email_voucher($_booking_number, $_hotelname_en,
$order_email , $first_name, $last_name);
$order = wc_get_order($order_id);
.
.
.And in _send_email_voucher function I use TCPDF to create PDF from an HTML file.
I use TCPDF beacuse the HTML file has Persian utf-8 characters, and TCPDF supports Persian characters.
function _send_email_voucher($_booking_number, $_hotelname_en, $_hotelname_fa, $_roomType_en, $_roomType_fa,
$_chekin_en, $_chekin_fa, $_chekout_en, $_chekout_fa, $_passengers, $_hotelstar,
$_hoteladdr, $_hotelphone, $_xtra_services, $_to_address, $_to_name, $_subject = "???? ???? ???",
$_depart_flight_number, $_depart_date_time, $_depart_flight_aircraft, $_depart_flight_airline,
$_return_flight_number, $return_date_time, $_return_flight_aircraft, $_return_flight_airline
){
ob_start();
include("voucher.php");
$_body = ob_get_contents();
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$pdf->setCreator(PDF_CREATOR);
$pdf->setAuthor('Nicola Asuni');
$pdf->setTitle('TCPDF Example 018');
$pdf->setSubject('TCPDF Tutorial');
$pdf->setKeywords('TCPDF, PDF, example, test, guide');
// set default header data
$pdf->setHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE.' 018', PDF_HEADER_STRING);
// set header and footer fonts
$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
// set default monospaced font
$pdf->setDefaultMonospacedFont(PDF_FONT_MONOSPACED);
// set margins
$pdf->setMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$pdf->setHeaderMargin(PDF_MARGIN_HEADER);
$pdf->setFooterMargin(PDF_MARGIN_FOOTER);
// set auto page breaks
$pdf->setAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
// set image scale factor
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
// set some language dependent data:
$lg = Array();
$lg['a_meta_charset'] = 'UTF-8';
$lg['a_meta_dir'] = 'rtl';
$lg['a_meta_language'] = 'fa';
$lg['w_page'] = 'page';
// set some language-dependent strings (optional)
$pdf->setLanguageArray($lg);
$pdf->setFont('dejavusans', '', 12);
$pdf->AddPage();
$pdf->setRTL(true);
$htmlpersian = '<span color="#660000">Persian example:</span><br />???? ??????? ???? PDF ????? ?? ??? ???? ?? ??. ???? ?? ?????.<br />???? ??? \"?\" ?? ???? ????? ????? ???? ???? ??? ?? ??? ??.<br />????? ???? ??? ? ??? ??? ?? ?? ??? ????? ??.<br />?? ???? ?? "Asuni Nicola" ? ???? ??? ?? ??? ???? ???????? ???? ?????.';
$pdf->WriteHTML($htmlpersian, true, 0, true, 0);
$pdf->Output('example_018.pdf', 'I');
.
.
.But I get the following error:
TCPDF ERROR:?Some data has already been output, can’t send PDF file
For solving problem I use ob_start, But the problem still persists.
I know taht the problem is in the Thankyou Page already been output, what I don’t know is ?ow do I make my function “send_thank_you_email” the first thing to be executed?
Or is there any other way to Email this PDF file without problem?
- You must be logged in to reply to this topic.