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

Tips on replacing wkhtmltopdf/KnpSnappyBundle with WeasyPrintBundle #4

Closed
myselfhimself opened this issue Jan 12, 2023 · 2 comments
Closed

Comments

@myselfhimself
Copy link

Hello,

for a use case of generating A4 from HTML pages (for email attachments), I have noticed while switching from KnpSnappyBundle to WeasyPrintBundle, the output font text ends up much bigger, while keeping an overall layout coherence:

KnpSnappyBundle:

	return $this->knpSnappy->getOutputFromHtml($html, array(
		'orientation' => 'portrait', // or Landscape
		'page-size' => 'A4',
		'encoding' => 'utf-8',
		'dpi' => 72,
		'enable-local-file-access' => true,
	));

WeasyPrintBundle:

       // for available options
       // @see \Pontedilana\PhpWeasyPrint\Pdf::configure
       return $this->weasyprintPdf->getOutputFromHtml($html, array(
           'encoding' => 'utf-8',
       ));
   }

with the following added to the source HTML <style> tag to "enforce" A4 page size

    @page {
        /* related: https://github.com/Kozea/WeasyPrint/issues/1579#issuecomment-1051689324 */
        size: 210mm 297mm;
    }

Would you have CSS/display/options tips for migrating?

@endelwar
Copy link
Member

Hi!
WeasyPrintBundle uses PhpWeasyPrint as php library which uses the Command-line API available from WeasyPrint.

In WeasyPrint documentation is stated that

WeasyPrint does not provide support for adjusting page size or document margins via command-line flags. This is best accomplished with the CSS @page at-rule

As you already found out adding @page CSS rule is the only option available.

For orientation option you can add it after size:

@page {
	size: A4 landscape;
}

For dpi option you can experiment with image-resolution (I've never tried it):

img { image-resolution: 300dpi }

I suggest to prepare a dedicated twig template and css for pdf generation, and don't recycle twig/html created for browser use.

@myselfhimself
Copy link
Author

Hello @endelwar , thank you for those tips which I have included in our current project; both rules you have stated do work OK.

For people looking into migrating away from wkhtmltopdf / knpsnappybundle, here is a related discussion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants