【问题标题】:How to adjust background image size of dompdf?如何调整dompdf的背景图片大小?
【发布时间】:2020-04-23 16:59:39
【问题描述】:

我正在 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的背景图片又不失清晰度?

【问题讨论】:

    标签: php html css laravel dompdf


    【解决方案1】:

    Dompdf,包括 0.8.4,不支持 background-size 声明(参考 issue 463)。

    由于您似乎希望将整个图像包含在容器中,您可以尝试在相对定位的 div 中使用具有低 z-index 的定位图像。您必须更精确地定义元素的位置,但它应该适用于您的示例。

    这是一个在 Dompdf 0.8.4 中呈现 OK 的最小化演示:

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

    【讨论】:

      【解决方案2】:

      试试这个

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

      【讨论】:

      • @SreyaSRV 然后不要在后台添加图像添加图像标签以获得良好的质量
      猜你喜欢
      • 1970-01-01
      • 2011-03-18
      • 2021-02-23
      • 2014-02-17
      • 1970-01-01
      • 1970-01-01
      • 2020-11-05
      • 1970-01-01
      • 2019-02-07
      相关资源
      最近更新 更多