I just started using dompdf v0.6.0beta3 and the dompdf Codeigniter helper with Codeigniter. I have copied the Arial font family tff files from Windows 7 font folder to dompdf's /lib/fonts folder.
Problem: When I select a text using CSS and apply the font-weight: bold property, on the HTML output it is indeed bold, but after converting to PDF via dompdf, the text is no longer in bold!
font-size: 24px does not work, all text in the pdf are the same sizes. And the only font being used in the pdf appears to be Times New Roman!
How can I make my text bold and change its size and font in the pdf?
PHP (Controller)
function pdf() {
$this->load->model('resume_model');
$results = $this->resume_model->get_resume_details($user_id);
$this->load->helper(array('dompdf', 'file'));
$html = $this->load->view('resume_pdf', $results, true);
pdf_create($html, 'filename');
}
Is your stylesheet external to your HTML content? If so you may just have a path problem. The plugin appears to use $dompdf->load_html() to load the document. DOMPDF has no knowledge of your website when used in this way and will work off the local file system. What this means for you is that file paths are relative to the currently-executing file. If the path is absolute (e.g. /css/main.css) then DOMPDF will look for this file off the root of the file system. Instead of looking for the file at /wwwroot/content/css/main.css it will look for /css/main.css.
The quickest fix, if this is your problem, would be to add a full URL, including domain, to your file references (e.g. http://example.com/css/main.css).
Related
When using PHP 8.1 and mpdf 8.1.2, when generating a PDF, the body of the resulting pdf is empty. While when I just output the HTML used in writehtml, I do get the full content including the css interpreted correctly.
However, when I comment out the <style>{% include '#App/inlineStyles/pdf.css' %}</style> part, it does render the PDF correctly, however (obviously) without the required styling.
I also tried seperately including both using 2 different writeHTML calls (with different modes for css). This resulted in the css not being applied (but the content being written).
The pdf.css file doesn't contain anything weird/invalid. It's partially based on tailwind and generated from .scss files.
I tried new Mpdf(['debug'=>true]) and also tried to catch any mpdfexceptions, but there were none.
Can somebody help me out?
EDIT:
The reason for this happening is within CssManager.php that the entire css gets removed:
// Remove CSS (tags and content), if any
$regexp = '/<style.*?>(.*?)<\/style>/si'; // it can be <style> or <style type="txt/css">
$html = preg_replace($regexp, '', $html);
EDIT2:
How am I supposed to include a file? I think I know the problem; I require the tailwind css component in my .css file which I'm including. The length of my .css file is 38601 lines (1000kb). The problem is in Mpdf/CssManager.php, line 481 (the code above). The return of the preg_replace is null because the file is too long.
In the past (with PHP 7.3 & an older version of mpdf) I did manage to include the tailwind css also into the mpdf writehtml. But in this version it does not work any longer.
My main question is, what is the best way to include the tailwind component? As I'm guessing that the length is what is causing $html to be null and therefore resulting in a blank page
(I'm not sure whether it would still be required to include a file (?))
I am working with laravael,I have an HTML view that has CSS integrated. I want to convert into a PDF. If I open the view (it doesn't mather if I open it via my Documents or by a link in my app) it works fine, everything looks ok. But if I get that file and generate a PDF with dompdf, when I open it the css for the background and the images are in their places but the texts change places and have another size.
Here is how I convert it to a PDF
$file = file_get_contents("../resources/views/panel/historial/pdfs/otros.html");
$dompdf = new DOMPDF();
$dompdf->loadHtml($file);
//$dompdf->load_html_file($html);
$dompdf->render();
$dompdf->stream("otros.pdf", array("Attachment" => 0));
return $dompdf;
enter image description here
I think that is not working in that way to except then the DOMPDF-Renderer has not the full CSS functionality.
https://github.com/dompdf/dompdf/wiki/CSSCompatibility
Here is a list of elements that are supported. So in your case i would suggest that you render a new template and make it with a different style for your PDF.
Another good solution is wkhtmltopdf which has a better support but is a command line tool which you have to call over php or if you don't need PHP then run it directly from your command line.
https://wkhtmltopdf.org/
I'm making a PDF with TCPDF, and I'm trying to make the file as small as possible. The font I'm using is Open Sans. I'm not (intentionally, at least) using Helvetica anywhere in the PDF. When I view the included fonts with Adobe Reader in my outputted PDF file, both Open Sans and Helvetica are listed. I have noticed that if I AddFont() other fonts, the outputted PDF gets bigger.
To save space, how can I tell TCPDF to not include Helvetica in the file?
The Helvetica font is added by TCPDF for two reasons:
On initialization the TCPDF class sets the default font to Helvetica (in the constructor) and therefore adds this font to the fonts list of the document.
For older versions:
To prevent this, you can edit the file config/tcpdf_config.php and change the constant PDF_FONT_NAME_MAIN to your desired default font name (should be around line 155). Note that you must not use any core font because they will never be embedded.
For newer versions:
Define PDF_FONT_NAME_MAIN with your desired default font name before including the TCPDF files. Example:
define('PDF_FONT_NAME_MAIN', 'freesans');
include_once 'path/to/tcpdf.php';
TCPDF adds an invisible link "Powered by www.tcpdf.org" at the bottom of the page.
To prevent this you have to use an override class like this:
class MyPdf extends TCPDF {
public function __construct($orientation='P', $unit='mm', $format='A4', $unicode=true, $encoding='UTF-8', $diskcache=false, $pdfa=false) {
// call parent constructor
parent::__construct($orientation, $unit, $format, $unicode, $encoding, $diskcache, $pdfa);
// disable the tcpdf link
$this->setTcpdfLink(false);
}
/**
* Allows to disable the invisible "Powered by www.tcpdf.org" link at the bottom of the page.
* #param type $tcpdflink
*/
public function setTcpdfLink($tcpdflink = true) {
$this->tcpdflink = $tcpdflink ? true : false;
}
}
The Helvetica font is one of the standard 14 core PDF fonts, so it is not embedded in the PDF when it is used. If you look in the TCPDF fonts directory you will notice that the Helvetica file only contains a description of the font and not a copy of the font. Therefore it shouldn't be significantly increasing the file size.
Solution
The Helvetica font is set as the default font in the TCPDF config files. From my testing, it appears that this causes it to be set as a font in the generated PDF files even when it is not used. Changing the default fonts in your TCPDF configuration files should prevent this from happening.
I have to face the same issue. I have tried the JOR solution. its correct but it still shows the Helvetica font family in my pdf.
for my pdf, I am using SVG image.so it displays the Helvetica . in the tcpdf.php protected property called $svgstyles has the SVG font family as Helvetica.
Just find $tcpdflink in tcpdf.php and make that variable false. This works for me
I'm trying to use HTML2PDF 4.03 with this code:
<?php
$content = "..."; # my HTML code
require_once(dirname(__FILE__).'/html2pdf_v4.03/html2pdf.class.php');
$html2pdf = new HTML2PDF('P','A4','en', true, 'utf-8', array(15,20,15,20) );
# here I'm trying to add my arial.ttf
$html2pdf->pdf->AddTTFFont('arial.ttf');
$html2pdf->WriteHTML($content);
$html2pdf->Output('exemple.pdf');
?>
Now the program die with this:
PHP Fatal error: Call to undefined method HTML2PDF_myPdf::AddTTFFont()
How can I add TTF font to my PDF file?
I have managed to add 1 custom font to my setup using the following method.
First convert the .ttf file to 3 separate files (.php .z and .ufm) using the following
font converter
Place the 3 files that are generated by this system into the fonts folder in TCPDF.
Now you can set the default font for your PDF using the following command
$html2pdf->setDefaultFont("the_name_you_called_your_font");
This was fairly simple to get working, I am having issues using 2 seperate fonts though via this method. I'll figure it out though
To expand on the selected answer (by o11y_75) when you convert your fonts, you need to use a specific name to include also the bold and italic variants.
That way, you only add one font definition like this
$html2pdf->AddFont('opensans', 'normal', 'opensans.php');
$html2pdf->setDefaultFont('opensans');
When you convert the fonts, name them, for example, like these:
default: opensans
bold: opensansb
italic: opensansi
bold italic: opensansbi
notice that behind the original name, you add b, i and bi on each case.
I have found no documentation on this issue, but I followed the nomenclature found on the fonts that already came with TCPDF and it worked.
If you want to add multiple fonts, just use :
$html2pdf->addFont('opensansregular', '', 'opensansregular');
$html2pdf->addFont('opensansbold', '', 'opensansbold');
I would suggest that you don't use special chars with the font converter specified above.
Then in your CSS simply type :
<style type="text/css">
<!--
.uppercase {
text-transform: uppercase;
}
* {
font-family: opensansregular;
}
h1, h2, h3, strong {
font-family: opensansbold;
}
-->
</style>
HTML2PDF works internally with TCPDF.
TCPDF has its own object since version 6.2.6 to create the fonts needed for HTML2PDF: TCPDF_FONTS
I have solved this as follows:
I searched for TCPDF in the vendor directory and found the Fonts directory there.
Then I created my own separate PDF script and used it once to create the necessary font files from the ttf.
usage: $fontname = TCPDF_FONTS::addTTFfont('vendor/tecnickcom/tcpdf/fonts/arialuni/arialuni.ttf', 'TrueTypeUnicode', '', 32);
For more details see: https://stackoverflow.com/a/70337995/2320007
Im scripting to generate a PDF file using TCPDF library, all I want now is to add font files into the document, I could add them using "SetFont" like
$pdf->SetFont('myfont', '', 10);
and its when I execute this script, the document in browser is rendering fonts perfectly, if I save it and open in another machine then fonts are working with, default fallback font is applying.
I did check it by going to File->Properties->Fonts in Adobe reader, font file is listed there, but text is missing the font.
Any idea will be helpful.
You will probably want to embed the font using $pdf->AddFont().
A longer description of the differences between SetFont(), AddFont() and AddTTFFont() here:
TCPDF, "Could not include font definition file" with OpenType fonts