0

I'm doing a project in Laravel in which student's certificate will be generated dynamically using Barryvdh DomPDF. This is my controller function:

public function certPdfGenerate() { ini_set('memory_limit', '-1'); $apply_id= input::get('id'); $get_certificate_data = DB::Connection('mysql1')->table('tbl_apply_certificate')->where('apply_id',$apply_id)->first(); $get_certificate_id1 = DB::Connection('mysql1')->table('tbl_apply_certificate')->select('apply_certificate_id')->where('apply_id',$apply_id)->get()->toArray(); foreach ($get_certificate_id1 as $key) { $get_certificate_id = $key->apply_certificate_id; } $filename = $get_certificate_id.'.pdf'; DB::Connection('mysql1')->table('tbl_apply_certificate') ->where('apply_id', $apply_id) ->update(['apply_pdf' => $filename]); $data1 = [$get_certificate_data]; $pdf=PDF::loadView('students::htmlcertificate', array('data' =>$data1), [], ['format' => 'A4-L']); $pdf->setPaper('A4', 'landscape'); $pdf->save(storage_path('certificates/'.$get_certificate_id.'.pdf')); return $pdf->stream('htmlcertificate.pdf'); }

This is my view page :

<style type="text/css">	#certbackground	{ background-image: url('http://localhost/folder_name/public/cer_template/final-certificate.png'); background-position: top left; background-repeat: no-repeat; background-size: 100%; padding: 300px 100px 10px 100px; width:100%; height:100%;	} #certbackground1 { background-image: url('http://localhost/folder_name/public/cer_template/final-certificate-back.png'); background-position: top left; background-repeat: no-repeat; background-size: 100%; padding: 300px 100px 10px 100px; width:100%; height:100%; } </style> </head> <body> <div id="certbackground">	@foreach ($data as $value)	<h3 style="margin-top: 142px;margin-left: 900px;font-weight: 100;">{{$value->apply_certificate_id}}</h3>	<h3 style="margin-top: 30px;margin-left: 370px;font-weight: 100;">{{$value->apply_name}}</h3>	<img style="height: 180px!important;margin-top: -5px;margin-left: 845px;" src="{{ URL::asset('public/certif_pro_image/'.$value->apply_photo) }}" />	<h3 style="margin-top: -148px;margin-left: 370px;font-weight: 100;">{{$value->apply_course}}</h3>	<h3 style="position:absolute;top:320px!important;margin-top:320px;margin-left: 450px;font-weight: 100;">{{$value->apply_softwares}}</h3>	<h3 style="position:absolute;top:175px!important;margin-top:175px;margin-left: 532px;font-weight: 100;">{{date('d-m-Y', strtotime($value->apply_start_date))}}</h3>	<h3 style="position:absolute;top:175px!important;margin-top:175px;margin-left: 670px;font-weight: 100;">{{date('d-m-Y', strtotime($value->apply_end_date))}}</h3>	<h3 style="position:absolute;top:189px!important;margin-top:189px;margin-left: 550px;font-weight: 100;">Grade {{$value->apply_grade}}</h3>	<h3 style="position:absolute;top:210px!important;margin-top:210px;margin-left: 440px;font-weight: 100;">{{$value->apply_centre}}</h3>	<img style="height: 80px!important;position:absolute;top:196px;margin-top:196px;margin-left: 220px;" src="{{ URL::asset('public/qrcode/'.$value->apply_qrcode) }}" />	<h5 style="position:absolute;top:545px;margin-left: 5px;font-weight: 100;">www.domain.in/certificate/{{$value->apply_certificate_id}}</h5>	@endforeach </div> <div style="page-break-after: always;"></div> <div id="certbackground1"> </div> </body>

The problem here is that the background image of pdf is too large in pdf. The size of background image is 3507 x 2480 with resolution 300. When I change the image size to 1058 x 748, it fits to pdf but the quality of pdf after print is too low.

How do we adjust the background image of pdf without losing clarity?

2 Answers 2

2

Dompdf, up through and including 0.8.4, does not support the background-size declaration (ref issue 463).

Since it looks like you want to contain the entirety of the image within the container you could try using a positioned image with a low z-index inside a relatively-positioned div. You would have to more precisely define the the positioning of the elements, but it should work for your example.

Here's a minimized demo that renders OK in Dompdf 0.8.4:

<div style="position: relative; border: 5px solid black; width: 450px; height: 375px; margin: auto;"> <img src="https://picsum.photos/200/150?grayscale" style="position: absolute; width: 450px; height: 375px; z-index: -1000;" /> <div style="position: absolute; top: 180px; width: 100%; text-align: center; color: white; line-height: 1.5"><span style="background-color: black; padding: .5em;">CONGRATULATIONS</span></div> </div> 
Sign up to request clarification or add additional context in comments.

Comments

0

try this

<style type="text/css"> #certbackground { background-image: url(''); background-position: top left; background-repeat: no-repeat; background-size: 100%; padding: 300px 100px 10px 100px; width:100%; height:100%; } #certbackground1 { padding: 300px 100px 10px 100px; width:300px; height:500px; } </style> </head> <body> <div id="certbackground"> @foreach ($data as $value) <h3 style="margin-top: 142px;margin-left: 900px;font-weight: 100;">{{$value->apply_certificate_id}}</h3> <h3 style="margin-top: 30px;margin-left: 370px;font-weight: 100;">{{$value->apply_name}}</h3> <img style="height: 180px!important;margin-top: -5px;margin-left: 845px;" src="{{ URL::asset('public/certif_pro_image/'.$value->apply_photo) }}" /> <h3 style="margin-top: -148px;margin-left: 370px;font-weight: 100;">{{$value->apply_course}}</h3> <h3 style="position:absolute;top:320px!important;margin-top:320px;margin-left: 450px;font-weight: 100;">{{$value->apply_softwares}}</h3> <h3 style="position:absolute;top:175px!important;margin-top:175px;margin-left: 532px;font-weight: 100;">{{date('d-m-Y', strtotime($value->apply_start_date))}}</h3> <h3 style="position:absolute;top:175px!important;margin-top:175px;margin-left: 670px;font-weight: 100;">{{date('d-m-Y', strtotime($value->apply_end_date))}}</h3> <h3 style="position:absolute;top:189px!important;margin-top:189px;margin-left: 550px;font-weight: 100;">Grade {{$value->apply_grade}}</h3> <h3 style="position:absolute;top:210px!important;margin-top:210px;margin-left: 440px;font-weight: 100;">{{$value->apply_centre}}</h3> <img style="height: 80px!important;position:absolute;top:196px;margin-top:196px;margin-left: 220px;" src="{{ URL::asset('public/qrcode/'.$value->apply_qrcode) }}" /> <h5 style="position:absolute;top:545px;margin-left: 5px;font-weight: 100;">www.domain.in/certificate/{{$value->apply_certificate_id}}</h5> @endforeach </div> <div style="page-break-after: always;"></div> <img id="certbackground1" src="http://localhost/folder_name/public/cer_template/final-certificate-back.png" /> </div> </body> 

1 Comment

@SreyaSRV then don't add image in background add in image tag for good quality