Home > Enterprise >  How to draw diagonal lines with SCRIPT in PDF
How to draw diagonal lines with SCRIPT in PDF

Time:02-06

I'm using laravel-dompdf package for generating pdf.

I could draw a diagonal line using rotate of the CSS. As you can see, my document have a lot of diagonal lines, so I want to draw a line with scripts.

Controller

public function downloadPDF () {
    $pdf = PDF::loadView('test', [
        'users' => $users //$users is array
    ])->setPaper('a4', 'portrait');
    return $pdf->download("test.pdf");
}

View(test.blade.php)

<!DOCTYPE html>
<html>
...
<body>
<table>
    <tbody>
        <tr>
            <th>{{-- Draw diagonal line here --}}</th>
            <th>Name</th>
            <th>TEL</th>
        </tr>
        @foreach($users as $key=>$user)
            <tr>
                <td>{{ $key }}</td>
                <td>{{ $user['name'] }}</td>
                <td>{{ $user['tel'] }}</td>
            </tr>
        @endforeach
    </tbody>
</table>    
</body>
</html>

Below is a sample pdf format. enter image description here

How can draw diagonal lines with script?

CodePudding user response:

I could use line function of Dompdf\Adapter\CPDFclass.

As PDF rendering interface, CPDF provide to enable php script on blade, using set_option("enable_php", true)

line function

line($x1, $y1, $x2, $y2, $color, $width, $style = [])

Controller

public function downloadPDF () {
    $pdf = PDF::getDomPDF()->set_option("enable_php", true)->loadView('test', [
        'users' => $users //$users is array
    ])->setPaper('a4', 'portrait');
    return $pdf->download("test.pdf");
}
<script type="text/php">
    if (isset($pdf)) {
        $pdf->page_script('
            $pdf->line(100, 100, 200, 200, array(0,0,0), 0.7);
        ');
    }
</script>
  • Related