I am using FPDF.php. I get an error when I try to create a PDF "FPDF error: Missing or incorrect image file: http://www.example.com/wp-content/uploads/2015/05/image.jpg"
In my error log I get this error:
PHP Warning: getimagesize(http://www.example.com/wp-content/uploads/2015/05/image.jpg): failed to open stream: Connection refused in /home/cluster-sites/2639/c/example.com/public_html/wp-content/themes/nameoftheme/includes/fpdf/fpdf.php on line 1213
I have set allow_url_fopen = ON in php.ini
I think it has something to do with the image path but I'm not sure what. It works fine on my test server.
My code is as follows:
//check if the query_string contains an ID
if(!isset($_REQUEST['id']))
{
header('Location:'.site_url());
exit();
}
//check if the ID is related to a WP_Post
$post = get_post($_REQUEST['id']);
if(empty($post))
{
header('Location:'.site_url());
exit();
}
//creating the brochure directory if not exists
if(!is_dir(ABSPATH.'wp-content/uploads/brochure'))
mkdir(ABSPATH.'wp-content/uploads/brochure');
$pdf_path = ABSPATH.'wp-content/uploads/brochure/'.$post->post_name.'.pdf';
$cache = false;
$debug = false;
//generating the pdf if not exists or older than the latest post update
if(!$cache || !is_file($pdf_path) || strtotime($post->post_modified) > filemtime($pdf_path))
{
require(get_template_directory().'/includes/fpdf/fpdf_enhanced.php');
global $imic_options;
$image_size = '600-400-size';
$pdf = new PDF_enhanced();
$pdf->AddFont('opensans','','opensans.php');
$pdf->AddFont('opensans','b','opensansb.php');
$pdf->AddFont('opensans','i','opensansi.php');
//$pdf->AddFont('opensans','eb','opensanseb.php');
//$pdf->AddFont('opensans','ebi','opensansebi.php');
$pdf->AddFont('opensans','l','opensansl.php');
//$pdf->AddFont('opensans','li','opensansli.php');
//$pdf->AddFont('opensans','s','opensanss.php');
//$pdf->AddFont('opensans','si','opensanssi.php');
$pdf->SetMargins(0, 0);
$property_sights = get_post_meta($post->ID, 'imic_property_sights', false);
//---------------------------------first page
$pdf->AddPage();
//main picture
$featured_image_url = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), $image_size);
if(!empty($featured_image_url))
$pdf->Image($featured_image_url[0], 0, 0, 210, 0);
else if(!empty($property_sights))
{
$image = wp_get_attachment_image_src(array_shift($property_sights), $image_size);
$pdf->Image($image[0], 0, 0, 210, 0);
}
//left border
$pdf->SetFillColor(183, 132, 36);
$pdf->Rect(0, 0, 1.2, 297, 'F');
//property title
$pdf->SetFont('opensans', '', 30);
$pdf->SetTextColor(97, 33, 114);
$pdf->SetXY(20, 150);
$pdf->MultiCell(175, 12, $post->post_title, $debug);
//property address
$address = get_post_meta($post->ID, 'imic_property_site_address', true);
$pdf->SetFont('opensans', 'i', 20);
$pdf->SetTextColor(102, 102, 102);
$pdf->SetXY(20, 175);
$pdf->Cell(100, 12, $address, $debug);
//property price
$pdf->SetFont('opensans', 'b', 16);
$pdf->SetTextColor(102, 102, 102);
$pdf->SetXY(120, 180);
$pdf->MultiCell(60, 12, html_entity_decode(imic_get_currency_symbol($imic_options['currency-select'])).number_format(get_post_meta($post->ID, 'imic_property_price', true)), $debug, 'R');
//property number beds
$beds = get_post_meta($post->ID, 'imic_property_beds', true);
$pdf->SetFont('opensans', 'b', 11);
$pdf->SetTextColor(102, 102, 102);
$pdf->SetXY(20, 200);
$pdf->Cell(30, 5.5, 'Number beds: ', $debug, 'L');
$pdf->SetFont('opensans', 'l', 11);
$pdf->SetTextColor(102, 102, 102);
$pdf->SetXY(50, 200);
$pdf->Cell(20, 5.5, html_entity_decode($beds), $debug, 'L');
//property number of baths
$baths = get_post_meta($post->ID, 'imic_property_baths', true);
$pdf->SetFont('opensans', 'b', 11);
$pdf->SetTextColor(102, 102, 102);
$pdf->SetXY(20, 208);
$pdf->Cell(36, 5.5, 'Number of baths: ', $debug, 'L');
$pdf->SetFont('opensans', 'l', 11);
$pdf->SetTextColor(102, 102, 102);
$pdf->SetXY(56, 208);
$pdf->Cell(20, 5.5, html_entity_decode($baths), $debug, 'L');
//property floor area
$area = get_post_meta($post->ID, 'imic_property_area', true);
$pdf->SetFont('opensans', 'b', 11);
$pdf->SetTextColor(102, 102, 102);
$pdf->SetXY(20, 216);
$pdf->Cell(23, 5.5, 'Floor area: ', $debug, 'L');
$pdf->SetFont('opensans', 'l', 11);
$pdf->SetTextColor(102, 102, 102);
$pdf->SetXY(43, 216);
$pdf->Cell(20, 5.5, html_entity_decode($area.' Sq Ft'), $debug, 'L');
//property beachfront
$beachfront = get_post_meta($post->ID, 'imic_property_beachfront', true);
$pdf->SetFont('opensans', 'b', 11);
$pdf->SetTextColor(102, 102, 102);
$pdf->SetXY(20, 224);
$pdf->Cell(25, 5.5, 'Beachfront: ', $debug, 'L');
$pdf->SetFont('opensans', 'l', 11);
$pdf->SetTextColor(102, 102, 102);
$pdf->SetXY(45, 224);
$pdf->Cell(20, 5.5, html_entity_decode($beachfront), $debug, 'L');
//property freehold
$pdf->SetFont('opensans', 'b', 11);
$pdf->SetTextColor(102, 102, 102);
$pdf->SetXY(20, 232);
$pdf->Cell(20, 5.5, 'Freehold: ', $debug, 'L');
$pdf->SetFont('opensans', 'l', 11);
$pdf->SetTextColor(102, 102, 102);
$pdf->SetXY(40, 232);
$pdf->Cell(20, 5.5, 'Yes', $debug, 'L');
//property close to
$closeto = implode(', ', get_post_meta($post->ID, 'imic_property_closeto', true));
$pdf->SetFont('opensans', 'b', 11);
$pdf->SetTextColor(102, 102, 102);
$pdf->SetXY(20, 240);
$pdf->Cell(19, 5.5, 'Close to: ', $debug, 'L');
$pdf->SetFont('opensans', 'l', 11);
$pdf->SetTextColor(102, 102, 102);
$pdf->SetXY(39, 240);
$pdf->MultiCell(150, 5.5, html_entity_decode($closeto), $debug, 'L');
//logo
$pdf->Image($imic_options['logo_upload']['url'], 150, 270, 45, 0);
//---------------------------------second page
$pdf->AddPage();
//second page main picture
if(!empty($property_sights))
{
$image = wp_get_attachment_image_src(array_shift($property_sights), $image_size);
$pdf->Image($image[0], 0, 0, 210, 0);
}
//save the next image for the third page
$third_page_image = false;
if(!empty($property_sights))
$third_page_image =array_shift($property_sights);
$height = 142;
//thumbnails
if(!empty($property_sights))
{
$height += 58;
$image = wp_get_attachment_image_src(array_shift($property_sights), $image_size);
$pdf->Image($image[0], 0, 142, 69, 0);
if(!empty($property_sights))
{
$image = wp_get_attachment_image_src(array_shift($property_sights), $image_size);
$pdf->Image($image[0], 71, 142, 69, 0);
}
if(!empty($property_sights))
{
$image = wp_get_attachment_image_src(array_shift($property_sights), $image_size);
$pdf->Image($image[0], 142, 142, 69, 0);
}
}
else
$height += 10;
//property title
$pdf->SetFont('opensans', '', 15);
$pdf->SetTextColor(97, 33, 114);
$pdf->SetXY(20, $height);
$pdf->MultiCell(180, 12, $post->post_title, $debug);
//property address
$address = get_post_meta($post->ID, 'imic_property_site_address', true);
$pdf->SetFont('opensans', 'i', 15);
$pdf->SetTextColor(102, 102, 102);
$pdf->SetXY(20, $height + 10);
$pdf->Cell(200, 12, $address, $debug, 'L');
//property features
$height += 25;
$pdf->SetFont('opensans', 'i', 10);
$pdf->SetTextColor(102, 102, 102);
$pdf->SetXY(25, $height);
$pdf->Cell(100, 2, 'Features: ', $debug);
$property_amenities = get_post_meta($post->ID, 'imic_property_amenities', true);
foreach($property_amenities as $properties_amenity)
{
if($properties_amenity != 'Not Selected')
{
$pdf->SetFillColor(102, 102, 102);
$pdf->SetFont('opensans', 'l', 10);
$pdf->SetTextColor(102, 102, 102);
$pdf->SetXY(25, $height);
$pdf->Cell(100, 12, $properties_amenity, $debug);
$height += 5;
}
}
//---------------------------------third page
$pdf->AddPage();
//second page main picture
if(!empty($third_page_image))
{
$image = wp_get_attachment_image_src($third_page_image, $image_size);
$pdf->Image($image[0], 0, 0, 210, 0);
}
//rest of description
$description = $post->post_content;
$description = preg_replace("/(\r?\n){2,}/", "\n", $description);
$description = str_replace('’', '\'', $description);
$description = apply_filters( '$description', $post->post_content );
if(strlen($description) > 900)
$description = substr($description, 0, strpos($description, '.', 900)).'.';
$pdf->SetFont('opensans', 'l', 11);
$pdf->SetTextColor(102, 102, 102);
$pdf->SetXY(20, 145);
$pdf->MultiCell(170, 5.5, iconv("UTF-8", "CP1250//TRANSLIT", $description), $debug, 'L');
//separator
$pdf->SetFillColor(183, 132, 36);
$pdf->Rect(20, 234, 170, 0.7, 'F');
//contact title
$pdf->SetFont('opensans', 'i', 12);
$pdf->SetTextColor(183, 132, 36);
$pdf->SetXY(19, 234);
$pdf->Cell(20, 10, 'Example Name', $debug);
//contact details
$contact = "Building\n1st Street W.I.\[email protected]\n+(1) 246 432 4663\nexample.com";
$pdf->SetFont('opensans', 'l', 9);
$pdf->SetTextColor(102, 102, 102);
$pdf->SetXY(19, 244);
$pdf->MultiCell(100, 4, $contact, $debug);
//---------------------------------fourth page
//save pdf
$pdf->Output($pdf_path);
}
//last check if the pdf exists
if(is_file($pdf_path))
{
//display pdf
header('Content-type: application/pdf');
readfile($pdf_path);
}
else
{
header('Location:'.site_url());
exit();
}
?>