我的生成的QR码在使用dompdf在laravel中显示在PDF中时为什么会变形?

huangapple go评论54阅读模式
英文:

Why is my generated QR code distorted when displaying it in a PDF with dompdf in laravel?

问题

你尝试在生成的PDF中显示一个生成的QR码,但出现了失真问题。即使在元素内或使用CSS类指定尺寸,仍然无法匹配这些尺寸。

在控制器中,你将QR码数据指定为:

'qr_code' => QrCode::size(300)->generate(route('donate', ['hash' => $hash], true)),

然后像这样加载视图:

$customPaper = array(0,0,626.50,1181.25);
$file = $pdf->loadView('admin.order.card', $data)->setPaper($customPaper, 'landscape')->setOptions(['dpi' => '200']);

你尝试直接在Blade文件中生成QR码,但效果相同。另外,你尝试在图像标签中直接添加宽度和高度参数,但没有效果。

这是它的外观:
我的生成的QR码在使用dompdf在laravel中显示在PDF中时为什么会变形?

你还尝试将它放在一个包装器内:

.qr-wrap {
    position: relative;
    width: 300px;
    height: 300px;
}

.qr {
    height: 100%;
    width: 100%;
    /*width: 3000px;*/
    /*height: 100px;*/
}

如果QR码失真,你可以尝试调整QR码的生成尺寸或检查生成QR码的库是否存在问题。另外,确保在生成PDF时没有对图像进行额外的缩放或变换。

英文:

I am trying to display a generated QR code inside my generated PDF but for some reason it's really distorted. Even when I specify the dimensions inside the element or as a class with CSS it does not match those dimensions.

My entire PDF page:

<html>
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <meta name="csrf-token" content="{{ csrf_token() }}">
        <title>Kaart</title>
        <style>
            body{
                margin: 0;
                white-space: nowrap;
            }
            .card{
                border:1px solid black;
                height: 1102px;
                position: relative;
                width: 1575px;
            }

            .card .left{
                width: 50%;
                border-right: 1px solid black;
                height: 100%;
                position: relative;
                display: inline-block;
            }

            .card .right{
                width: 50%;
                height: 100%;
                position: relative;
                display: inline-block;
            }

            .card .right .text{
                position: relative;
                top: 50%;
                left: 50%;
                transform: translate(-50%, -50%);
                text-align: center;
                white-space: normal;
                width: 80%;
            }

            .card .left img{
                width: 100%;
                height: 100%;
            }

            .text .top{
                font-size: 50px;
            }

            .text .bottom{
                font-size: 30px;
            }

        </style>
    </head>
    <body>
    @foreach($cards as $key => $card)
        <div class="card">
            <div class="left">
                <img src="{{ $card['card_image'] }}">
            </div>
            <div class="right">
                <div class="text">
                    <div class="top">
                        <p>{!! nl2br($card['message']) !!}</p>
                    </div>
                    <div class="bottom">
                        <p>Scan de QR-code of ga naar {{ $card['donate_url'] }} om te doneren aan een goed doel naar keuze.</p>
                        <div class="qr-wrap">
                            <img class="qr" src="data:image/png;base64,{{ base64_encode($card['qr_code']) }}" />
                        </div>
                        <p>Waarde: <b>{{ $card['wallet_amount'] }}</b></p>
                    </div>
                </div>
            </div>
        </div>
        @if(($key + 1) !== $total)
            <div class="page-break"></div>
        @endif
    @endforeach
    </body>
</html>

In my controller I specify the qr data as: 'qr_code' => QrCode::size(300)->generate(route('donate', ['hash' => $hash], true)),

And then load the view like this:

$customPaper = array(0,0,626.50,1181.25);
$file = $pdf->loadView('admin.order.card', $data)->setPaper($customPaper, 'landscape')->setOptions(['dpi' => '200']);

I've tried directly generating the QR inside the blade file but this has the same effect. Also I've tried to add width and height parameters directly in the image tag but no effect.

This is what it looks like:

我的生成的QR码在使用dompdf在laravel中显示在PDF中时为什么会变形?

I've also tried adding it inside a wrapper:

   .qr-wrap{
                position: relative;
                width: 300px;
                height: 300px;
            }
            .qr{
                height: 100%;
                width: 100%;
                /*width: 3000px;*/
                /*height: 100px;*/
            }

答案1

得分: 2

I use the following QR Code generator:

我使用以下的QR码生成器:

<img alt="QR Code" src="data:image/png;base64, {!!
      base64_encode(\SimpleSoftwareIO\QrCode\Facades\QrCode::format('png')
      ->size(60)->generate($qr_code)) !!} ">

你也可以尝试设置背景颜色,这有助于调试不同的HTML元素。

英文:

What QR Code generator do you use? Maybe there is an second size option?

This works for me:

&lt;img alt=&quot;QR Code&quot; src=&quot;data:image/png;base64, {!!
      base64_encode(\SimpleSoftwareIO\QrCode\Facades\QrCode::format(&#39;png&#39;)
      -&gt;size(60)-&gt;generate($qr_code)) !!} &quot;&gt;

Also you could try to set background-colors. It helps to debug the different HTML elements.

huangapple
  • 本文由 发表于 2023年4月13日 17:38:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76003936.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定