Export php file output to PDF file (Hebrew output) - php

Im tried to export the output of my php file.
For example, I have this php file:
<?php
echo "שלום!";
?>
I tried the tcpdf class, in the expamples folder there is a file that crate pdf with html code, the example 61 or 6 here.
The problem is when I try to take the html output by file_get_content and the content is in Hebrew the content of the pdf is "???????" or somthing like this.
Any idea how I can export my hebrew output to pdf?
I also tried fpdf and others libraries and I didn't succeed.
Help please.
Thanks

font-family: firefly, DejaVu Sans, sans-serif;
Use this font-family attribute on your hebrew letter contain element.

Do yourself a favor - use snappy to create PDF from HTML, unlike dompdf it supports all languages, directions etc.
(The package itself is just a wrapper, it uses an external library - wkhtmltopd to create the pdf).
It will render pretty much anything in any language, Hebrew included -
https://github.com/KnpLabs/snappy

Related

mPDF generating blank pdf when importing css

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 (?))

PHP - Dynamically creating a svg with embedded fonts

I want to dynamically create an svg that I can use in an <img/> tag. This in itself is easy; create an svg, set the header and echo the generated parts in their correct place.
The problem is, I want to be able to embed fonts in the svg.
I've tried using the #font-face rule in the css of the svg, but that didn't work (MDN says that it only works on Android and Safari).
Is there any cross-browser way to do this?
Solutions I've Considered:
Possible Solution #01:
The solution:
In my main file, create an svg file which uses the #font-face css rule, and then use exec() to use inkscape to convert that svg into another svg, which converts all letters into paths. I then could use echo file_get_contents($inkscape_file) with the correct headers to output it as a svg which can be used with an <img/> tag.
The problem with this:
This creates 2 additional files, so seems very inefficient. Furthermore, since each user will end up generating several images, the space it takes up would grow phenomenally.
Possible Solution #02:
The solution:
Make a template in illustrator, then save it as svg, and tick the embed all glyphs option. Then replace the text & the styles with the options from the PHP script. Use the correct header and output this.
The problem with this:
This severely limits the amount of fonts that can be used, as it is limited to only those which I create a template for. My desired behaviour was to add the option for users to upload their own fonts and use them. This solution does not allow for that.
Additional information that may be of some relevance:
My development server runs fedora, and the production server uses redhat.
The #font-face rule I am currently using is as follows:
#font-face {
font-family: Potato;
src: url("/fonts/potato.otf");
}
You can't load any external resources declared in the svg from the <img> tag.
The only solutions would be some crappy ways to append the glyphs or the fonts into the svg file itself.
Actually there is a not so crappy way to do it as you found in this answer by lèse-majesté.
The best way is then still IMO to not use an <img> tag to display the svg documents, but rather use an <iframe> or an <object> tag, with the #font-face declared inside the svg file, or even directly include an inline version into the document. These methods do allow the loading of external resources such as fonts.
Then you just have to save the fonts on your server or just an url to the font in the #font-face declaration.

FPDF Text with HTML tags

I'm using FPDF to create an A4 sized PDF, I can add text/images and this is working fine. I have a dynamically generated string which contains some HTML tags (BR etc) and was wondering how I can have that output onto the PDF as opposed to literally printing the tags out.
$centre = urldecode($row_CertDetails['centre_name']);
$pdf->Text(10, 110, $centre);
I've seen on some forums people mentioning WriteHTML, however my version of FPDF doesn't seem to include that.
Is it an add-on?
Yes, it is an Add-On:
Write HTML
Author: Clément Lavoillotte
License: FPDF
This is an enhancement of the WriteHTML() method from tutorial 6. Supported tags are: ...
Please see Write HTML FPDF Script #42 which I think is the latest version, but feel free to browse the FPDF scripts/Add-Ons.
I've solved it anyway.
Because I'm also using FPDI, what I did was extract the contents of the class in http://www.fpdf.org/en/script/script42.php and put it inside the FPDI.php file.

Making text bold or larger in dompdf

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).

Convert text to PDF in PHP

What would be the simplest, shortest way to turn a text file into a PDF file with PHP? With some basic example code if possible.
I've seen this but the examples don't show how to use a text file as input.
http://uk3.php.net/manual/en/book.pdf.php
Thanks
TCPDF and FPDF can both render PDF output. If you're on a Linux system, you could also call the system's ghostscript to do it.
You'll definitely need a library to write PDFs. I'd say try FPDF.
TCPDF is the best way to convert text or HTML to PDF.
http://www.tecnick.com/public/code/cp_dpage.php?aiocp_dp=tcpdf
Throwing Zend_Pdf into the ring as well.
And a somewhat old tutorial to get you started in addition to the extensive docs in the ZF manual:
http://devzone.zend.com/article/2525
You can use MPDF.
I wrote a smalll instruction:
1) download Full version(http://www.mpdf1.com/mpdf/index.php?page=Download ) and extract inside anyfolder
2) create sample.php in anyfolder and insert like this:
<?php
include('./mpdf.php');
$mpdf=new mPDF();
$mpdf->WriteHTML('<div style="color:yellow;">SAMPLE TEXT, WITH HTML TAGS Too!</div>');
$mpdf->Output(); die('');
?>
3) then open sample.php in your browser to see the result!
MANY OTHER ONES - Convert HTML + CSS to PDF with PHP?

Categories