hromby's avatar

PDF with CSS styling

Hi!

Has anyone tried to create PDF's with a link to a stylesheet in a Homestead environment?

I have tried using both barryvdh's and thujohn's packages, but they seem to ignore the link to the CSS. They both work if the CSS is in the content of the page itself, but when the CSS is in a separate document, it is ignored.

Does anyone know of a package that works with homestead?

0 likes
8 replies
puzbie's avatar
puzbie
Best Answer
Level 4

Oddly enough I'm coming at this from the other direction. I was using a pdf generator that worked in tandem with an external HTML rendering engine,but am now trying out thujohn's package.

The original package I was using was: knplabs/knp-snappy

So you might want to give that one a try. You do have to configure the rendering engine too though, so its slightly less trivial to use.

Or alternatiely, why not read the css file yourself and insert it into the html you want the pdf to be generated from?

hromby's avatar

Grabbing the CSS from the file works, thanks!

However, I am using Bootstrap as a grid for the PDF, and it does not work at all. I can set different sizes of the columns, but they always stack -- they never align on the same row. How can I get a grid system to use in a PDF?

henrique's avatar

The first problem may be folder permission, I had this while trying to use an external font, it caches the file somewhere around vendor/thujohn/pdf/i dont know... It threw an exception though.

As for the grid, it (domPdf) don't support (100%) floats and some other directives, so it can't align the grid properly, there is a config ('DOMPDF_ENABLE_CSS_FLOAT' => false), which you may want to try setting to true, but it's experimental and may break more than it fixes :( When I had to use grid, I just showed the HTML and let the user download it as pdf with Chrome (I use Zurb Foundation and had to add some code to adjust for @media print), else you could try... argh... tables :/ (which is what I did on another project).

hromby's avatar

I think I'll just use tables. It's not something I'm proud of, but it does the job. I have a couple of graphs and stuff I need to align in rows with different numbers of columns, and while it would have been nice to have a real grid, tables do the job.

I won't be able to show the HTML and download as PDF as they are generated through a cronjob and sent with email.

Thanks for the help!

headersalreadysent's avatar

Is there any print media related css in your page? Maybe you can check and delete them if there is in css.

puzbie's avatar

Knowing how to do layouts in tables is still a useful skill, as it is useful if you ever need to send html email.

And its not as if you are ever going to have to have your pdf file validated. So go on... bet dirty!

hromby's avatar

Just following up on this: I managed to use Bootstrap by using barryvdh/laravel-snappy. Also, the missing styles were due to me not referencing the full path :-)

1 like
puzbie's avatar

Yes, Snappy certainly renders better than thujohn's package. For instance, if you write a function to draw a absolutely positioned DIV, with a border, and specify top, left, width and height, then you get issues. You can draw a border from 10,30 to 30,40, then draw another one from 30,40 to 50, 60, then the 30,40 in the first box will be different from the 30,40 in the second. Yet if you render it as normal HTML in the browser then it works fine.

Still, it served its purpose for what I needed, so I can't say fairer than that.

Please or to participate in this conversation.