dompdf special characters - dompdf

I'm having successful html-to-pdf conversions, but not with special characters.
Below is just a special character I'm trying to display, which displays in browsers on my Mac, when I put it simply inside an html document. (but not on my windows box)
<?php
require_once("../dompdf_config.inc.php");
$html = '€';
$dompdf = new DOMPDF(); $html = iconv('UTF-8','Windows-1250',$html);
$dompdf->load_html($html);
$dompdf->render();
$dompdf->stream("contract.pdf");
exit(0);
?>
I keep getting a "?" (question mark) when the pdf is rendered.
I know there's been lots of issues documented with regards to special characters, but I thought I'd give this a try, with the code I'm actually using.
If DomPdf isn't a recommended html-to-pdf conversion tool, I'll take any other recommendations!

I have experienced problems with DOMPDF when converting an UTF-8 html page.
I simply solved the problem by adding
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
Between < head > tag.
Maybe it could be an alternative if you set it with your encoding type.
IMPORTANT NOTE from comments below: don't use stream() and output() methods on the same pdf instance. If you do this wont work.

after trying all solutions on the net. I could solve without modifying the dompdf. the problem was on the html content. I just had to add the correct and appropriate HTML structure and setting the font of course . Tested on v0.6.0 and v0.6.1. here I leave the code
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="charset=utf-8" />
<style type="text/css">
* {
font-family: "DejaVu Sans Mono", monospace;
}
</style>
</head>
<body>your content ćčžšđ...</body>
</html>

DOMPDF Latin Turkish (Türkçe) char problem, my solution %100 Work.
Server requirenment control:
Char 'dejavu sans mono' (Turkish support) OR:
Step 1: dompdf_config.inc.php edit to
mb_internal_encoding('UTF-8');
def("DOMPDF_UNICODE_ENABLED", true);
Step 2: lib/fonts/dompdf_font_family_cache.dist.php edit to add code:
'futural' =>
array (
'normal' => DOMPDF_FONT_DIR . 'FUTURAL',
'bold' => DOMPDF_FONT_DIR . 'FUTURAL',
'italic' => DOMPDF_FONT_DIR . 'FUTURAL',
'bold_italic' => DOMPDF_FONT_DIR . 'FUTURAL',
),
Step 3: doqnload font files to copy lib/fonts folder.
Download font files Link http://www.2shared.com/file/k6hdky_b/fonts.html
Step 4: Your code Edit example:
require_once("dompdf_config.inc.php");
$html='<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<style>
html{padding:-30px;}
body { font-family: futural; }
</style>
</head><body>';
$html.='ı İ Ş ş ç Ç ö Ö ü Ü ğ Ğ ÿ þ ð ê ß ã Ù Ñ È » ¿ İsa Şahintürk';
$html.='</body></html>';
if ( isset( $html ) ) {
if ( get_magic_quotes_gpc() )
$html = stripslashes($html);
$old_limit = ini_set("memory_limit", "16M");
$dompdf = new DOMPDF();
$dompdf->load_html($html,'UTF-8');
$dompdf->set_paper('a4', 'portrait');// or landscape
$dompdf->render();
$dompdf->stream("limitless.pdf");
exit(0);
}
End Finish PDF > example
http://limitsizbilgi.com/pdf/dompdf-chartest.pdf

Anything prior to 0.6.x has limited support for characters outside iso-8859-1 encoding. The Euro is supported in 0.5.x by using the appropriate Windows ANSI character code (€), but otherwise you have to jump through some PDF encoding hoops.
The 0.6.0 release has better support for "special" characters. The default encoding is based on Windows ANSI (one of the few recognized by the PDF 1.3 spec). You can enable better character support by loading a Unicode-based font and enabling Unicode in dompdf and specifying that encoding in your document.
The following should work in dompdf 0.6.0 or greater:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
</head>
<body>
<p>€</p>
</body>
</html>
(or to be lazy just use the euro entity € in your test)
There is a document outlining the steps needed to enable Unicode support in DOMPDF.
Plus read this answer for an overview of how to load fonts.

You must use another character set. For example dejavu Sans Mono. Add your code
<style>
*{
font-family:"DeJaVu Sans Mono",monospace;
}
</style>
I also mentioned it in this video.

Related

Cannot display Japanese in Pdf file after using by Laravel-dompdf

I'm trying to convert a blade file to PDF format.
I've used barryvhd/laravel-dompdf to convert to PDF but DOMPdf doesn't support Japanese font, all Japanese character display to ? character.
My php code:
$pdf = PDF::loadView('pdf/presaleorder', ['order' => $order]);
$pdf->save('test.pdf');
My blade file:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>画像</title>
</head>
<body>
<div>
<p>
ァ ア ィ イ ゥ ウ ェ エ ォ オ カ ガ キ ギ ク
グ ケ ゲ コ ゴ サ ザ シ ジ ス ズ セ ゼ ソ ゾ タ
ダ チ ヂ ッ ツ ヅ テ デ ト ド ナ ニ ヌ ネ ノ ハ
バ パ ヒ ビ ピ フ ブ プ ヘ ベ ペ ホ ボ ポ マ ミ
ム メ モ ャ ヤ ュ ユ ョ ヨ ラ リ ル レ ロ ヮ ワ
ヰ ヱ ヲ ン ヴ ヵ ヶ ヷ ヸ ヹ ヺ ・ ー ヽ ヾ ヿ
</p>
</div>
</body>
</body>
</html>
However, I found a post solved this. I've added <style> in header
<style>
#import url('https://fonts.googleapis.com/css?family=Roboto');
body {
font-family: 'Roboto', sans-serif;
}
</style>
Now, all character display like this:
But when I copied text in pdf file, I got this
ァ ア ィ イ ゥ ウ ェ エ ォ オ カ ガ キ ギ ク グ ケ ゲ コ ゴ サ ザ シ ジ ス ズ セ ゼ ソ ゾ タ ダ チ ヂ
I wonder, it's because of barryvhd/laravel-dompdf or PDF Reader cause this. I'm using Adobe Acrobat Reader DC version 2020.012.20043 and installed font-pack.
Anyone has solution?
Character sets
You need to define the character set in the meta tag in the HTML document.
<meta charset="UTF-8" />
Finally, I can solve this problem. It's because Roboto font does not support for Japanese. Instead use fonts which support Japanese. I've tried MS Mincho, Hiragano Sans and it worked.
A note that dompdf only support for font with format .ttf, so other format will not work.

What PDF meta data does dompdf support?

When using dompdf what PDF meta data can be set in the document information dictionary?
Originally asked elsewhere:
Are you able to parse more META info to be added to PDF information during PDF generation?
/Creator (DOMPDF)
/CreationDate (D:20150818031116-05'00')
/ModDate (D:20150818031116-05'00')
Can you specify Author, Copyright, etc..?
I cannot find ANY reference to this. Only just saw your: Creator, Creation Date and Modification Date!
In the current stable release (0.6.1) the HTML <title> element and some <meta> elements (author, keywords, description) are used to set the relevant PDF meta data.
<html>
<head>
<title>Ehhhhhhh</title>
<meta name="author" content="Arthur Herbert Fonzarelli">
<meta name="keywords" content="fonzie, cool, ehhhhhhh">
</head>
<body>
<p>Ehhhhhhh</p>
</body>
</html>
In addition, you can add other info to the PDF using the $dompdf->add_info() method. The full list of supported metadata info that you can set is: Title, Author, Subject, Keywords, Creator, Producer, CreationDate, ModDate, Trapped.
$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->render();
$dompdf->add_info('Subject', 'Cool');
$dompdf->add_info('Title', 'Your meta Title');
$dompdf->add_info('Author', 'Your meta Author');
$dompdf->add_info('Subject', 'Your meta Subject');
$dompdf->add_info('Keywords', 'Your meta Keywords');

Javascript and character encoding

In my ASP.NET MVC 3 project, I have set the character encoding in my master page
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
then, in my view, I have
<script type="text/javascript" charset='UTF-8'>
$(function () {
$('#my-btn').click(function () {
$(this).val('#MyProject.Resources.OrderButton');
});
});
</script>
what gives me the value Zamów onstead of Zamów. The resource file's first line is:
<?xml version="1.0" encoding="utf-8"?>
Any ideas how to fix it ?
The correct way to pass server side values to javascript variables is the following:
var value = #Html.Raw(Json.Encode(MyProject.Resources.OrderButton);
$(this).val(value);
This will output code which is completely safe and correctly encoded to be passed to a javascript function. This will also properly handle cases where your string contains characters such as ', new lines, ... which would have broken your javascript code.
And you should not care whether some characters are HTML or whatever encoded. The important thing is that they will be correctly encoded for a browser or an HTML compliant client to correctly consume.

Change CKEditor default newpage_html

Here's my scenario: I have CKEditor with docprops enabled and fullpage set true. With fullpage true the contentsCss does nothing, that's a "won't fix" in Trac.
I'm trying to modify the NewPage code that gets replaced. Currently (with fullpage true), this is what clicking NewPage enters:
<!DOCTYPE html />
<html>
<head>
<title></title>
</head>
<body></body>
</html>
That's great, but there is no way to edit it. Using newpage_html only enters code into the body tags, not replacing the whole thing.
I want to replace the entire code so I can declare my CSS defaults which I can't do since fullpage is enabled.
I've looked high and low and I can't find out how to modify this. I can't even find where the default code is coming from in the source code! Any help would be glorious!
Use config.newpage_html (it was missing from the docs)
CKEDITOR.replace( 'editor1',
{
fullPage : true,
extraPlugins : 'docprops',
newpage_html : '<!doctype html><html><head><title>My test</title>' +
'<link href="sample.css" rel="stylesheet" type="text/css" />' +
'</head><body>New page, type here</body></html>'
});

MVC 3 Razor View Engine - script blocks appear before DOCTYPE

I have a very strange problem. I have migrated my views from Webforms view engine to Razor. I am finding now that when the html for my page is rendered, it doesn't render the DOCTYPE at the top (as it should), but rather renders some javascript script blocks before the DOCTYPE tag. I have no clue what is causing this. The result it that the browser displays the page in Quirks mode. This manifests by my font-size in my tables not conforming to the font-size set for the body tag.
I must also mention that I am using Telerik MVC extensions version 2011Q1.
Below is a portion of the page source from the beginning of the html page to the end of the head tag. Any help on why this is happening will be appreciated.
<script type="text/javascript" src="/asset.axd?id=PQEAAB-LCAAAAAAABADsvQdgHEmWJSYvbcp7f0r1StfgdKEIgGATJNiQQBDswYjN5pLsHWlHIymrKoHKZVZlXWYWQMztnbz33nvvvffee--997o7nU4n99__P1xmZAFs9s5K2smeIYCqyB8_fnwfPyJ-8Uezjx597xd_tPro0Uevp3WxapuPRh-d82dL-uynf9E6r6-3d8f3x7vjn8Z31UePdn7JSL5t8zKvi7fjabVYVEv7_W73-zZ_106qd7bBXrfBRV3M7Lf3zLfS-fgyK4tZ1ua2wX709XxWtMXywra638MimzQtDdG2-PSXfP-XfH_00bTlRu_auz-dXWYNU4EaXNKnezu7uzTwe7v36YMpkerep_fpl48etfU6_yX_TwAAAP__IQbpFT0BAAA%3d"></script>
<script type="text/javascript">
//<![CDATA[
jQuery(document).ready(function(){
if (!jQuery.telerik) jQuery.telerik = {};
jQuery.telerik.cultureInfo={"shortDate":"dd/MM/yyyy","longDate":"dd MMMM yyyy","longTime":"HH:mm:ss","shortTime":"HH:mm","fullDateTime":"dd MMMM yyyy HH:mm:ss","sortableDateTime":"yyyy\u0027-\u0027MM\u0027-\u0027dd\u0027T\u0027HH\u0027:\u0027mm\u0027:\u0027ss","universalSortableDateTime":"yyyy\u0027-\u0027MM\u0027-\u0027dd HH\u0027:\u0027mm\u0027:\u0027ss\u0027Z\u0027","generalDateShortTime":"dd/MM/yyyy HH:mm","generalDateTime":"dd/MM/yyyy HH:mm:ss","monthDay":"dd MMMM","monthYear":"MMMM yyyy","days":["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"],"abbrDays":["Sun","Mon","Tue","Wed","Thu","Fri","Sat"],"abbrMonths":["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec",""],"months":["January","February","March","April","May","June","July","August","September","October","November","December",""],"am":"AM","pm":"PM","dateSeparator":"/","timeSeparator":":","firstDayOfWeek":1,"currencydecimaldigits":2,"currencydecimalseparator":".","currencygroupseparator":",","currencygroupsize":3,"currencynegative":1,"currencypositive":0,"currencysymbol":"£","numericdecimaldigits":2,"numericdecimalseparator":".","numericgroupseparator":",","numericgroupsize":3,"numericnegative":1,"percentdecimaldigits":2,"percentdecimalseparator":".","percentgroupseparator":",","percentgroupsize":3,"percentnegative":0,"percentpositive":0,"percentsymbol":"%"};
jQuery('#CoursesGrid').tGrid({columns:[{"title":"Id","member":"Id","type":"Number","editor":null},{"title":"Course Title:","member":"Title","type":"String","editor":null},{"title":"Completion Category","member":"CompletionCategory","type":"String","editor":null},{"title":"Expiry Months (0 to 100):","member":"ExpiryMonths","type":"Number","editor":null},{"title":"Commands","commands":[{"name":"edit","buttonType":"Image"},{"name":"delete","buttonType":"Image"}]}], plugins:["editing"], editing:{"mode":"InForm","editor":"\r\n\r\n\u003cdiv\u003e\r\n \u003cfieldset class=\"editfieldset\"\u003e\r\n \u003clegend class=\"titlelegend\"\u003eCourse Details\u003c/legend\u003e\r\n \u003col\u003e\r\n \u003cli\u003e\r\n \u003clabel for=\"Title\"\u003eCourse Title:\u003c/label\u003e \r\n \u003cinput id=\"Title\" name=\"Title\" type=\"text\" value=\"\" /\u003e \r\n \u003cspan class=\"field-validation-valid\" id=\"Title_validationMessage\"\u003e\u003c/span\u003e \r\n \u003c/li\u003e\r\n \u003cli\u003e\r\n \u003clabel for=\"Description\"\u003eDescription:\u003c/label\u003e \r\n \u003ctextarea cols=\"20\" id=\"Description\" name=\"Description\" rows=\"2\"\u003e\r\n\u003c/textarea\u003e \r\n \u003cspan class=\"field-validation-valid\" id=\"Description_validationMessage\"\u003e\u003c/span\u003e \r\n \u003c/li\u003e\r\n \u003cli\u003e\r\n \u003clabel for=\"ExpiryMonths\"\u003eExpiry Months (0 to 100):\u003c/label\u003e \r\n \u003cdiv class=\"t-widget t-numerictextbox\"\u003e\u003cinput class=\"t-input\" id=\"ExpiryMonths\" name=\"ExpiryMonths\" style=\"width:100%\" value=\"0\" /\u003e\u003ca class=\"t-link t-icon t-arrow-up\" href=\"#\" tabindex=\"-1\" title=\"Increase value\"\u003eIncrement\u003c/a\u003e\u003ca class=\"t-link t-icon t-arrow-down\" href=\"#\" tabindex=\"-1\" title=\"Decrease value\"\u003eDecrement\u003c/a\u003e\u003c/div\u003e\u003cscript type=\"text/javascript\"\u003e\r\n\tjQuery(\u0027#ExpiryMonths\u0027).tTextBox({val:0, step:1, minValue:-2147483648, maxValue:2147483647, digits:0, groupSize:3, negative:1, text:\u0027Enter value\u0027, type:\u0027numeric\u0027});\r\n\u003c/script\u003e\r\n \r\n \u003cspan class=\"field-validation-valid\" id=\"ExpiryMonths_validationMessage\"\u003e\u003c/span\u003e \r\n \u003c/li\u003e\r\n \u003c/ol\u003e\r\n \u003c/fieldset\u003e\r\n\u003c/div\u003e\r\n","defaultDataItem":{"Id":0,"Title":null,"Description":null,"CompletionCategory":null,"ReminderId":0,"ExpiryMonths":0,"Deleted":false,"ScheduledCourses":[]}}, dataKeys:{"Id":"id"}, validationMetadata:{"Fields":[{"FieldName":"Title","ReplaceValidationMessageContents":true,"ValidationMessageId":"Title_validationMessage","ValidationRules":[{"ErrorMessage":"Course Title is required.","ValidationParameters":{},"ValidationType":"required"}]},{"FieldName":"Description","ReplaceValidationMessageContents":true,"ValidationMessageId":"Description_validationMessage","ValidationRules":[]},{"FieldName":"ExpiryMonths","ReplaceValidationMessageContents":true,"ValidationMessageId":"ExpiryMonths_validationMessage","ValidationRules":[{"ErrorMessage":"The Expiry Months (0 to 100): field is required.","ValidationParameters":{},"ValidationType":"required"},{"ErrorMessage":"The field Expiry Months (0 to 100): must be a number.","ValidationParameters":{},"ValidationType":"number"}]}],"FormId":"CoursesGridform"}, pageSize:0, sortMode:'single', ajax:{"selectUrl":"/Course/_IndexAjax","insertUrl":"/Course/_InsertAjax","updateUrl":"/Course/_UpdateAjax","deleteUrl":"/Course/_DeleteAjax"}, localization:{"addNew":"Add new record","delete":"Delete","cancel":"Cancel","update":"Update","insert":"Insert","edit":"Edit","select":"Select","page":"Page ","displayingItems":"Displaying items {0} - {1} of {2}","pageOf":"of {0}","filter":"Filter","filterAnd":"And","filterClear":"Clear Filter","filterDateEq":"Is equal to","filterDateGe":"Is after or equal to","filterDateGt":"Is after","filterDateLe":"Is before or equal to","filterDateLt":"Is before","filterDateNe":"Is not equal to","filterNumberEq":"Is equal to","filterNumberGe":"Is greater than or equal to","filterNumberGt":"Is greater than","filterNumberLe":"Is less than or equal to","filterNumberLt":"Is less than","filterNumberNe":"Is not equal to","filterShowRows":"Show rows with value that","filterStringEndsWith":"Ends with","filterStringEq":"Is equal to","filterStringNe":"Is not equal to","filterStringStartsWith":"Starts with","filterStringSubstringOf":"Contains","groupHint":"Drag a column header and drop it here to group by that column","filterEnumEq":"Is equal to","filterEnumNe":"Is not equal to","deleteConfirmation":"Are you sure you want to delete this record?","filterSelectValue":"-Select value-","filterBoolIsFalse":"is false","filterBoolIsTrue":"is true","noRecords":"No records to display.","cancelChanges":"Cancel changes","saveChanges":"Save changes","refresh":"Refresh","sortedAsc":"sorted ascending","sortedDesc":"sorted descending","unGroup":"ungroup"}, noRecordsTemplate:'No records to display.'});
jQuery('#TabStrip').tTabStrip();});
//]]>
</script>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1">
<meta http-equiv="Content-type" content="text/html;charset=UTF-8" />
<title>Vigilaris Solutions</title>
<link rel="stylesheet" href="/Content/Themes/Shared/vway-backend.css" type="text/css" />
<link rel="stylesheet" href="/Content/Themes/Green/branding.css" type="text/css" />
<link rel="stylesheet" href="/Content/Themes/Shared/StatusBar.css.css" type="text/css" />
<link type="text/css" href="/asset.axd?id=lAAAAB-LCAAAAAAABADsvQdgHEmWJSYvbcp7f0r1StfgdKEIgGATJNiQQBDswYjN5pLsHWlHIymrKoHKZVZlXWYWQMztnbz33nvvvffee--997o7nU4n99__P1xmZAFs9s5K2smeIYCqyB8_fnwfPyJ-8Uezjx597xd_tPro0Ucn1bLNl-1Ho4_O-bMlfdbmZV4Xb8fTarGoluNp09DX1UePdn7JKGxwVSxn1VXzwDXZ_SXf_yXfH300bbnVu_aufHNJf-7t7O6Od8f3du_TB1PC4N6n9-mXjx619Tr_Jf9PAAAA__9JtaUdlAAAAA%3d%3d" rel="stylesheet"/>
</head>
Okay. I have resolved the problem. It is, in fact, a Telerik related issue. The issue is as follows:
In my layout view, my migrated razor view engine script registrar code looked as follows:
#{Html.Telerik().ScriptRegistrar()
.Globalization(true)
.DefaultGroup(g => g.Combined(true).Compress(true))
.Render();
}
This is a code block approach and results in the javascript script blocks being rendered right at the beginning of the page source before the DOCTYPE declaration, thus causing the browser to go into quirks mode (uggglllyyy!).
So, I changed the Telerik code in my layout view to the following:
#(Html.Telerik().ScriptRegistrar()
.Globalization(true)
.DefaultGroup(g => g.Combined(true).Compress(true))
)
The script blocks now correctly get rendered at the end of the page source and the browser no longer operates in quirks mode.
I really hope this can help other developers.
This is caused by something writing those scripts directly to the output stream (Response.Write). Razor uses Writer property of ViewContext for output before flushing everything to Response.
i take it you are using the Telerik MVC Components, more specifically the tabs?
check your view templates, specifically _Layout.cshtml and ensure you arent calling the telerik library before your markup.

Resources