Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CSS not loading on generated PDF #452

Closed
MZotti opened this issue May 9, 2022 · 2 comments
Closed

CSS not loading on generated PDF #452

MZotti opened this issue May 9, 2022 · 2 comments
Labels

Comments

@MZotti
Copy link

MZotti commented May 9, 2022

  • PHP: 7.3;
  • Laravel: 7.29;
  • knplabs/knp-snappy: 1.4;

Hello,

There's some issue ocurring when snappy creates the PDF, and the CSS supose to load on the view is not working.

This is the view with the correct CSS:

snappy_1

And this is the generated PDF:

snappy_2

The snappy code is configured this way:

public function createWithMultipleKits(Collection $kits, $disk = "local")
{
    $base_path = self::BASE_PATH . "/many";
    $size = ['100mm', '80mm'];
    $snappy = $this->configHtmlToPdf($size);
    $filename = 'etiquetas_internas_' . Str::random('6');
    $fileFullLocation = $base_path . "/{$filename}.pdf";
    $renderTicketHtml = view("pdfs.kit.ticket.index", ["kits" => $kits->map(function ($kit) {
        return [
            "id" => $kit->id,
            "number" => $kit->number,
            "type" => $kit->kit_type->name,
            "batch" => $kit->batch,
            "qrcode" => QrCode::generate(env("APP_FRONTEND_URL") . "/cadastrar-kit?kit_number=" . $kit->number)
        ];
    })->toArray()]);
    if (Storage::disk('local')->exists($fileFullLocation)) {
        Storage::disk('local')->delete($fileFullLocation);
    }
    $snappy->generateFromHtml($renderTicketHtml, storage_path("app") . "/" . $fileFullLocation, ['encoding' => 'UTF8']);
    return [$fileFullLocation, "$filename.pdf"];
}

private function configHtmlToPdf($size = ['100mm', '100mm'])
{
    list ($heigth, $width) = $size;

    $snappy = new Pdf('wkhtmltopdf');
    $snappy->setOption('header-spacing', '0');
    $snappy->setOption('enable-local-file-access', true);
    $snappy->setOption('margin-top', '0mm');
    $snappy->setOption('margin-bottom', '0mm');
    $snappy->setOption('margin-left', '0cm');
    $snappy->setOption('margin-right', '0cm');
    $snappy->setOption('header-font-size', '3');
    $snappy->setOption('footer-spacing', '5');
    $snappy->setOption('footer-line', true);
    $snappy->setOption('footer-font-size', '3');
    $snappy->setOption('page-height', $heigth);
    $snappy->setOption('page-width', $width);

    return $snappy;
}

The CSS is coded directly on the view file (inside the head tag).

@nikolinmajmari
Copy link

Could it be because wkhtml2pdf has some issues with flexbox.
If so check here for more info https://stackoverflow.com/questions/57020675/using-flex-css-with-wkhtmltopdf

@stale
Copy link

stale bot commented Nov 12, 2022

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Nov 12, 2022
@stale stale bot closed this as completed Jan 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants