Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

vinubangs's avatar

how to convert html to pdf by DOM-PDF with proper HTML design

I am using DOM-PDF for converting HTML to PDF in LARAVEL 5.4 PDF conversion is good. But HTML page design is disturbing. Means After click on "Download to pdf", HTML is converting into PDF. But HTML design in PDF is not in proper way.

showalumni.blade.php:

@extends('layouts.mainsectioninner')

@section('content')
<div class="panel-body panel-body-com-m">
<a href="{{ URL('pdf/'.$showalumni->id) }}">Download to pdf</a>
<hr>
  <label>Name : </label>
  <input type="text" class="form-control1 control3" value="{{ $showalumni->name }}"  required readonly readonly>
  <label>Father's Name :  </label>
  <input type="text" class="form-control1 control3" value="{{ $showalumni->f_name }}"  required readonly>
  <label>Gender :  </label>
  <input type="text" class="form-control1 control3" value="{{ $showalumni->gender }}"  required readonly>
  <label>College :  </label>
  <input type="text" class="form-control1 control3" name="colleges_name" value="{{ $showalumni->showcollege->colleges_name }}"  required readonly />
  <label>Course :  </label>
  <input type="text" class="form-control1 control3" placeholder="Course name...." name="courses_name" value="{{ $showalumni->showcourse->coursename }}"  required readonly />
</div>
@endsection

route:

Route::resource('registeredalumnies','Admin\RegisteredAlumniesController');
Route::get('pdf/{id}','Admin\RegisteredAlumniesController@download');

Controller:

public function show($id)
{

    $showalumni = User::where('id',$id)->with('showcollege','showcourse')->first();
    return view('Admin.showalumni',compact('showalumni'));
}

public function download($id)
{

    $showalumni = User::where('id',$id)->with('showcollege','showcourse')->first();
    $pdf = PDF::loadView('Admin.showalumni', compact('showalumni'));
    return $pdf->stream('invoice.pdf');
}
0 likes
5 replies
petrit's avatar
petrit
Best Answer
Level 20

You design depends on your css style. Maybe defining setPaper will help a little

...
public function download($id)
{
    $showalumni = User::where('id',$id)->with('showcollege','showcourse')->first();
    return PDF::setPaper('a4', 'portrait')->loadView('Admin.showalumni', compact('showalumni'))->stream('invoice.pdf);
}

otherwise, refer to this link here https://github.com/dompdf/dompdf/wiki

vinubangs's avatar

@PETRIT - Thanks for suggestion. setPaper() is not working with my query.

But I think, that link given by you should help me.

I have a another question. When I click on anchor to convert into PDF. Then it is taking too much time. Why?

petrit's avatar

@VINUBANGS - It is related to your server resources. It is normal taking much longer time to load pdf view than loading only a view.

Prit_Patel's avatar

translated text not proper in Pdf file getting the error converted text

Please or to participate in this conversation.