Как настроить размер фонового изображения dompdf? - PullRequest
0 голосов
/ 06 января 2020

Я делаю проект в Laravel, в котором сертификат студента будет генерироваться динамически с использованием Barryvdh DomPDF. Это моя функция контроллера:

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');
    }

Это моя страница просмотра:

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

Проблема в том, что фоновое изображение pdf слишком велико в pdf. Размер фонового изображения составляет 3507 x 2480 с разрешением 300. Когда я изменяю размер изображения на 1058 x 748, он умещается в pdf, но качество pdf после печати слишком низкое.

Как настроить фоновое изображение из pdf без потери четкости?

Ответы [ 2 ]

0 голосов
/ 21 января 2020

Dompdf, начиная с версии 0.8.4 и выше, не поддерживает декларацию background-size (ref , проблема 463 ).

Поскольку создается впечатление, что вы хотите содержать все изображение внутри контейнера, вы можете попробовать использовать позиционированное изображение с низким z-индексом внутри относительно позиционированного div. Вам нужно было бы более точно определить расположение элементов, но это должно работать для вашего примера.

Вот минимизированная демонстрация, которая отображает OK в 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>
0 голосов
/ 06 января 2020

попробуйте

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

...