I am trying to use custom font in dompdf while generating pdf file. As i found that custom font works only after generating .ufm and cache file.
Drupal php file
public function GeneratePdf() {
$dompdf = new Dompdf();
$fontDirectory = $host.'/'.$themepath . '/fonts';
$options->setChroot($fontDirectory);
$dompdf->setOptions($options);
ob_end_clean();
$dompdf->getFontMetrics()->registerFont(
['family' => 'montserrat', 'style' => 'normal', 'weight' => 'normal'],
$fontDirectory . '/Montserrat-Regular.ttf'
);
$dompdf->loadHtml($html);
$dompdf->render();
ob_end_clean();
}
Pdf generation HTML .twig file.
<html>
<head>
<style type="text/css">
#page {
size: auto;
margin: 0;
padding: 0;
}
#font-face {
font-family: montserrat;
src: url($themepath ~'/fonts/Montserrat-Regular.ttf') format('truetype'); font-style: normal;
}
</style>
</head>
<body style="margin:0; padding:0;">
<span style="font-family:'montserrat';color:#417bac; font-size:18px">PDF font testing</span>
</body>
</html>
Issue is after registering font through php function required files i.e .ufm and dompdf_font-family_cache.php not created in dompdf folder vendor\dompdf\dompdf\lib\fonts
Related
ViewBlade
<!doctype html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<style type="text/css" media="all">
.HVMC{font-family: "Herr Von Muellerhoff", cursive !important}
.MDHC{font-family: "Mr De Haviland", cursive !important;}
.ABC{font-family: "Alex Brush", cursive !important;}
.CCC{font-family: "Cedarville Cursive", cursive !important;}
.LBAC{font-family: "La Belle Aurore", cursive !important;}
.img-list {
background-size: contain;
background-position: center;
background-repeat: no-repeat;
position: relative;
width: 100%;
height: 1310px;
}
html,body
{
height: 100%;
margin: 0;
background-color:lightgray;
}
</style>
</head>
<div id="maindiv">
#foreach ($proposalfromTemplate as $proposal)
{!! $proposal->html !!}
#endforeach
<div>
</html>
Controller
PDF::setOptions(['isRemoteEnabled' => TRUE, 'enable_javascript' => TRUE]);
$pdf = PDF::loadView('pdfview');
return $pdf->download($pid . '.pdf');
When I return View It works fine, but when I return $pdf->download() it not including CSS, in Blade file
the given data is a text file which I get from storage so I am not able to add CSS on it
Thanks
You have to render your HTML first before load into DOMPDF
public function generateXYZ()
{
PDF::setOptions(['isRemoteEnabled' => TRUE, 'enable_javascript' => TRUE]);
$dompdf = new Dompdf();
$html = view('your.view.path')->render();
$dompdf->loadHtml($html);
$dompdf->render();
return $pdf->download('card.pdf');
}
I'm using wkhtmltopdf to generate my pdf files.
I have main page and footer page that I am rendering. They are in separate view.
Problem with printing it is that just font is loaded and other is not.
I did the main page the same way and all of the css is loaded correctly.
My view of footer.html.twig
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Statement Footer</title>
<link href="https://fonts.googleapis.com/css?family=Courier+Prime&display=swap" rel="stylesheet">
</head>
<style type="text/css">
html {
height: 0;
background-color: white;
white-space: nowrap;
font-family: 'Courier Prime', monospace;
}
hr {
display: block;
height: 1px;
border: 0;
border-top: 1px solid #ccc;
margin: 1em 0;
padding: 0;
}
#footer{
display:table-cell;
width:33.33vw;
text-align: center;
}
</style>
<body>
<hr/>
<div id="footer">
Hello!
</div>
</div>
</body>
</html>
And in my controller:
$options = [
'encoding' => 'UTF-8',
'footer-html' => $footerHtml = $this->renderView('#WebsiteTemplates/invoice/footer.html.twig')
];
$html = $this->renderView('#WebsiteTemplates/invoice/invoice.html.twig');
$filename = sprintf('test-%s.pdf', date('Y-m-d'));
return new Response(
$this->get('knp_snappy.pdf')->getOutputFromHtml($html, $options),
200,
[
'Content-Type' => 'application/pdf',
'Content-Disposition' => sprintf('attachment; filename="%s"', $filename),
]
);
Style tags are good, and I have no css files as assets that I have to include. All of them are directly in a twig.
I create a pdf in Symfony via snappy from a twig template:
So this is my Controller, where I am creating the pdf:
/**
* #Route("/pdf", name="pdf")
*/
public function pdf(Request $request, Pdf $snappy)
{
$snappy->setOption("encoding","UTF-8");
$html = $this->renderView("default/pdf.html.twig",array(
"title" => "pdf"
));
$filename = "mypdf";
return new Response(
$snappy->getOutputFromHtml($html),
200,
array(
'Content-Type' => 'application/pdf',
'Content-Disposition' => 'inline; filename="'.$filename.'.pdf"'
)
);
}
And here is the html template, where I create the header
pdf.html.twig:
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
* {box-sizing: border-box;}
body {
margin: 0;
font-family: Arial, Helvetica, sans-serif;
}
.header {
overflow: hidden;
background-color: #000;
padding: 20px 10px;
}
.header-right {
float: right;
}
}
</style>
</head>
<body>
<div class="header">
Logo
</div>
<div style="padding-left:20px">
<h1>headline</h1>
<p>Some text</p>
<p>Some content..</p>
</div>
</body>
</html>
I try that the black header fits into the page without border. But there is still a white border. It does not go to the edge.
Change this line
$snappy->setOption("encoding","UTF-8");
into
$snappy->setOption("encoding","UTF-8");
$snappy->setOption('margin-left', '0mm');
$snappy->setOption('margin-right', '0mm');
$snappy->setOption('margin-top', '0mm');
The issue is that wkhtmltopdf defaults to non-zero left & right margins. If you modify your executable to wkhtmltopdf -R 0 -L 0 ... the black will extend to the width of the page. See this.
Btw, you may want to remove the extra brace } at the line immediately above </style>
I need a pdf to be generated with name being placed on top of an image at a particular place. I have written a working HTML code but mpdf is unable to load the stylesheets and the name is getting printed below the image.
Here is my code of php:
<?php
$html = file_get_contents('index12.html');
require_once __DIR__ . '/vendor/autoload.php';
$mpdf = new \Mpdf\Mpdf();
$stylesheet = file_get_contents('style.css');
$mpdf->WriteHTML($stylesheet, 1);
$mpdf->WriteHTML($html,2);
$mpdf->Output();
index12.html
<!-- <link rel="stylesheet" href="style.css"> -->
<div class="image">
<img src="yugmak.jpg" alt="" />
<h2>Kung Fu Panda</h2>
</div>
style.css
#font-face {
font-family: "Montez";
src: url("Montez.ttf");
}
.image {
position: relative;
width: 100%; /* for IE 6 */
}
h2 {
position: absolute;
font-family: "Montez";
font-size: 250px;
top: 2100px;
padding-left: 1050px;
width: 2000px;
}
Any way to make the css work in pdf would be great.
I'm using Kohana 3 and pdfview/DOMPDF to generate pdf files but they are generated with 0 bytes and text/plain mime-type.
Controller:
public function action_pdf() {
if(isset($_POST['dados'])) {
$pdf = View_PDF::factory('export/pdf');
$pdf->title = '';
$pdf->headers = array();
$pdf->data = array();
$this->request->response = $pdf;
$this->request->send_file(true, 'dados.pdf');
}
}
View:
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<style type="text/css">
* {
border: 0;
margin: 0;
padding: 0;
}
table {
border: 1px solid;
border-collapse: collapse;
margin: 0 auto;
text-align: center;
width: 500px;
}
td {
border: 1px solid;
padding: 5px;
}
</style>
</head>
<body>
<h1>Teste</h1>
<table>
<tr></tr>
</table>
</body>
</html>
When I download the file and open it in epdfview (PDF viewer), it says:
Unable to open document
File type plain text document (text/plain) is not supported
I just don't know what's wrong. Thank you.
UPDATE:
I downloaded the last beta version and of DOMPDF, deleted the pdfview Kohana's module and did something like that in my controller:
public function action_pdf() {
if(isset($_POST['dados'])) {
require_once(Kohana::find_file('vendor', 'dompdf/dompdf_config.inc'));
$view = View::factory('report/pdf');
$view->title = '';
$view->data = array();
$pdf = new DOMPDF();
$pdf->load_html($view->render());
$pdf->render();
$pdf->stream('dados.pdf', array('Attachment' => 1));
}
}
Now it's working. Thanks!
I ran into a similar issue, and got this "dompdf File type plain text document (text/plain) is not supported" message. I checked out the install directions, and it had to do with changing file/folder permissions on the dompdf directory (lib/fonts is what was specifically mentioned in the install instructions). Works now.
It seems to be work When I changed the folder permission of folder dompdf
sudo chmod -rf 777 dompdf