0

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?

bluestar0505
  • 338
  • 1
  • 14
  • Please share more details, like the code you are currently using – Nico Haase Feb 02 '23 at 14:53
  • 1
    Does https://stackoverflow.com/questions/15727675/create-diagonal-border-of-a-cell help? – Nico Haase Feb 02 '23 at 15:15
  • or otherwise https://stackoverflow.com/questions/28425574/slanted-diagonal-line-in-html-or-css – Nanne Feb 02 '23 at 15:15
  • @NicoHaase Some CSS do not work in dompdf. – bluestar0505 Feb 02 '23 at 15:17
  • Feel free to add all clarification **to your question** by editing it. Did you try to use any CSS so far? – Nico Haase Feb 02 '23 at 15:19
  • @NicoHaase I tried. This is something that only people who have used ```laravel-dompdf ``` can feel. – bluestar0505 Feb 02 '23 at 15:28
  • 2
    dompdf should support `rotate` just fine, so "Some CSS do not work in dompdf" might be true, but not relevant here? Show what you tried please. (That said, dompdf is also ancient, it only really supports CSS 2.1 with a small amount of CSS3, and HTML4 instead of HJTML5, which is tech from 20+ years ago). – Mike 'Pomax' Kamermans Feb 02 '23 at 17:06
  • @Mike'Pomax'Kamermans You are right. ```rotate```is working on.As you can see, I want to draw a line with script because my document has a lot of diagonal lines. – bluestar0505 Feb 02 '23 at 17:28
  • @NicoHaase Thanks. ```rotate``` are working on dompdf. As you can see, my document have a lot of diagonal lines, so I want to draw a line with scripts. – bluestar0505 Feb 02 '23 at 17:34
  • 1
    You can't, because you can't "draw lines" in HTML, you have to either create an element and use CSS to make it _look_ like a line (e.g. border and transform) or you create a canvas element and draw a line on _that_ instead. No idea if dompdf supports canvas though, that would be up to you to check. – Mike 'Pomax' Kamermans Feb 02 '23 at 18:26
  • @Mike'Pomax'Kamermans I found a clue. I will use ```Dompdf\Adapter\CPDF::line()``` in blade. – bluestar0505 Feb 02 '23 at 18:28
  • Please add all attempts to your question by editing it. You haven't shared any CSS you are using – Nico Haase Feb 02 '23 at 18:57

1 Answers1

0

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>
bluestar0505
  • 338
  • 1
  • 14