0

I have tried to use TCPDF whose code is given below:

require_once($_SERVER['DOCUMENT_ROOT'].'/tcpdf/tcpdf.php'); 
$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 061');
$pdf->SetSubject('TCPDF Tutorial');
$pdf->SetKeywords('TCPDF, PDF, example, test, guide');
$pdf->setPrintHeader(false);
$pdf->setPrintFooter(false);
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
if (@file_exists($_SERVER['DOCUMENT_ROOT'].'/tcpdf/config/lang/eng.php')) {
    require_once($_SERVER['DOCUMENT_ROOT'].'/tcpdf/config/lang/eng.php');
    $pdf->setLanguageArray($l);
}
$pdf->SetFont('helvetica', '', 10); 
$html ="";
$pdf->AddPage();
$html .='<style>'. file_get_contents('http://localhost/Projects/PDF/css/style_4.css').'</style>';
$html .= file_get_contents("http://localhost/Projects/PDF/mycon.php"); 
$pdf->writeHTML($html, true, false, true, false, '');
$pdf->lastPage();
$pdf->Output('example_061.pdf', 'I');

The above code generates a PDF successfully but is not taking my CSS and fonts that I have defined into "style_4.css" and included externally.

I have also tried to include CSS on the page itself "mycon.php" but its not taking from there as well. What am I doing wrong?

Note: I have different fonts which will be dynamically attached with layout based on certain conditions.

halfer
  • 19,824
  • 17
  • 99
  • 186
Deepak Goswami
  • 2,030
  • 1
  • 16
  • 22
  • You could try PrinceXML, but a server license is very expensive indeed (though I think a free version is available for certain kinds of use). I understand it is considered to be the most robust web-to-PDF solution available, in terms of its implementation of paged media. However, you could try `wkhtmltopdf` as well - that uses WebKit internally and so may be okay for your needs. Bear in mind though that broad requests for libraries tend to be off-topic here, since such questions show their age quickly. – halfer Mar 26 '14 at 14:24
  • @halfer thanks for your reply ,Now I have updated my question, please check it now! And I have also tried with your suggestion but did not get any good help so that i can implement that. – Deepak Goswami Mar 27 '14 at 11:44
  • That's now on-topic IMO, so I'll cast a re-open vote. If you want to carry on researching the topic, you want to **embed fonts** - so if you search for "TCPDF embed fonts" you may find the answer. (It may however turn out [to be a duplicate](http://stackoverflow.com/search?q=embed+fonts+tcpdf)). – halfer Mar 27 '14 at 11:55
  • @halfer what if I have used multiple fonts in my page? – TheMohanAhuja Mar 27 '14 at 12:00
  • @TheMohanAhuja: Just `$pdf->addTTFfont()` each of them - you'll get a different font handle from each of the calls. – Piskvor left the building Mar 27 '14 at 12:23
  • i have checked for how to embed fonts it is working fine for single fonts but i have multiple fonts used in my web page so do i need to add them separately and also i have a doubt that can't be used all that fonts from css itself because this is for one page, so I will add required fonts but what if i will use same code for generate pdf for another url ? – Deepak Goswami Mar 27 '14 at 12:28
  • @DeepakGoswami: From your comments on an answer, you seem to have fallen into an XY problem - you seem to _actually_ want a PDF screenshot of a webpage. Next time, please include your end goal in the problem description. See also: http://meta.stackexchange.com/a/66378/19746 – Piskvor left the building Mar 27 '14 at 14:52
  • I am not getting what do you mean by "PDF screenshot of a webpage." ? – Deepak Goswami Mar 27 '14 at 14:56
  • @DeepakGoswami: In goes an URL address, out comes a PDF file. The PDF file contains the webpage as it would display in a web browser. – Piskvor left the building Mar 27 '14 at 15:38

1 Answers1

2

See TCPDF's documentation:

Using the addTTFfont() method you can directly create a TCPDF font starting from a TrueType, OpenType or Type1 font. NOTE: The 'fonts' folder must be writeable by the webserver.

For example:

$fontname = $pdf->addTTFfont('/path-to-font/DejaVuSans.ttf', 
                                'TrueTypeUnicode', '', 32);
Piskvor left the building
  • 91,498
  • 46
  • 177
  • 222