2

I'm using "pdfcrowd HTML to PDF API for PHP" to generate PDF files from the PHP and smarty file. I'm able to generate the PDF but not able to put the background watermark image to the PDF I'm printing out. I tried everything but still couldn't put the background watermark image. Can anyone please guide me regarding this? Where I'm going wrong? Any kind of help would be greatly appreciated. Following is my code for your reference:

<?php
  set_time_limit(0);

  require_once("../../includes/application-header.php");
  require_once OCN_ROOT."/core/extention/pdfcrowd/pdfcrowd_config.php";
  require_once OCN_ROOT."/core/extention/pdfcrowd/pdfcrowd.php";

try
  {   
    // create an API client instance
    $client = new Pdfcrowd($username, $apikey);
$objPracticeSheet = new PracticeSheet();
$practice_sheet_id = $_GET['practice_sheet_id'];

    $bread_crumbs_text = 'View Practice Sheet';

    $practice_details = $objPracticeSheet->ViewPracticeSheet($practice_sheet_id);

    $practice_sheet_details = $objPracticeSheet->GetPracticeSheetDetailsById($practice_sheet_id);

    $smarty->assign('bread_crumbs_text', $bread_crumbs_text);
    $smarty->assign('practice_sheet_name', $practice_sheet_details['practice_sheet_name']);
    $smarty->assign("practice_details", $practice_details);
    $smarty->assign("practice_sheet_questions", $practice_sheet_details['practice_sheet_total_questions']);
    $smarty->assign("practice_sheet_display_date", $practice_sheet_details['practice_sheet_display_date']);
    $smarty->assign("practice_sheet_for", $practice_sheet_details['practice_sheet_for']);

    $file_to_show = "pdf-practice-sheet.tpl";

    $final_data = $smarty->fetch($file_to_show);

    $file = fopen(ADMIN_ROOT."temp/pdf_practice_sheet.html", "w+");
    file_put_contents($file, "");

    fwrite($file, $final_data);
    fclose($file);  
  $client->setHeaderHtml($header_html);
    $client->setFooterHtml("<table border='0' width='100%' cellpadding='5' cellspacing='0' style='font-size:10px;font-family:verdana;background:#EFBC8F;margin-top:10px;'><tr><td align='left'>Powered by EntrancePrime.com : 
        India's Favourite Online Test Series for JEE, NEET, & CA-CPT</td><td align='right'>Page %p/%n</td></tr></table>");
    $client->setPageWidth("210mm");
    $client->setPageHeight("298mm");    
    $client->setNoCopy('True');
    //$client->setVerticalMargin("0.8in");
    $client->setPageMargins("1.1in","0.2in","0.4in","0.2in");

    //Set watermark
    $client->setWatermarkInBackground(True);
    $client->setWatermark("'".ADMIN_SITE_URL."assets/img/watermark.png'", -28, 406);

    $pdf = $client->convertFile(ADMIN_ROOT."temp/pdf_practice_sheet.html");
    $filename = $practice_sheet_details['practice_sheet_name'];
    // set HTTP response headers
    header("Content-Type: application/pdf");
    header("Cache-Control: no-cache");
    header("Accept-Ranges: none");
    header("Content-Disposition: attachment; filename=\"" . $filename . ".pdf\"");
    // send the generated PDF 
    echo $pdf;
}
catch(PdfcrowdException $why)
{
  echo "Pdfcrowd Error: " . $why;
}
?>

Looking forward to the reply.

PHPLover
  • 1
  • 51
  • 158
  • 311
  • What exactly is "not working"? – tereško Nov 20 '13 at 07:31
  • @tereško:I'm not able to put the background watermark imgae in the PDF generated. That's the only issue I'm facing. If you have any way to resolve this issue it would be very helpful to me. – PHPLover Nov 20 '13 at 07:33

1 Answers1

0

Try using an absolute URL(http://www.yoursite.com/image.png) in this $client->setWatermark("'".ADMIN_SITE_URL."assets/img/watermark.png'", -28, 406);

And see what you get. It looks like a path issue to me mate. Good luck.

Godinall
  • 2,280
  • 1
  • 13
  • 18
  • :The path is not an issue. I've defined a constant to keep uniformity in code and made the configuration job simpler. If tried by putting the bare URL in a address bar of a browser and it's displaying me the image. So certainly there is no issue with image path. – PHPLover Nov 20 '13 at 08:06
  • You didn't get the point. What is your constant then? With or without the / in the end? If the image displays with bare URL then it means your code was correct, you just didn't provide the right path for it to pick up. – Godinall Nov 20 '13 at 12:13
  • :I got the point very well. If I print the constant ADMIN_SITE_URL, it echoes http://localhost/abc/pqr/web/control/ and if I print the whole path then it prints 'http://localhost/eprime/entprm/web/control/assets/img/upload_users.png' The string in single quotes. So my point is there is no any issue regarding the path. Some other issue is there. – PHPLover Nov 20 '13 at 12:32