html2pdf.js icon indicating copy to clipboard operation
html2pdf.js copied to clipboard

CSS rules are not getting applied

Open ermarkar opened this issue 6 years ago • 21 comments

Thanks for the library. I was using jspdf that was showing black screen. This lib works fine but, the css rules are not getting applied. This is how I am using this with Angular

Installed

yarn add html2pdf.js

and then in my .ts file

import * as html2pdf from "html2pdf.js";

and

var opt = {
            margin: 1,
            filename: 'Invoice.pdf',
            image: { type: 'jpeg', quality: 0.98 },
            html2canvas: { scale: 2 },
            jsPDF: { unit: 'in', format: 'letter', orientation: 'portrait' }
};

html2pdf(__domElement, opt);

And this is the DOM element

original

and this is the output

ss

any solutions?

ermarkar avatar May 05 '19 12:05 ermarkar

Could it be CORS related?

brendon avatar May 15 '19 04:05 brendon

@brendon i don't think so ? Any solution though ?

ermarkar avatar May 15 '19 05:05 ermarkar

It looks like your style file isn't being downloaded. I believe that stuff is refetched via html2canvas. Froala passes this extra bit of config to html2pdf:

html2canvas: {
  useCORS: true
}

Give that a try :)

brendon avatar May 15 '19 22:05 brendon

It looks like your style file isn't being downloaded. I believe that stuff is refetched via html2canvas. Froala passes this extra bit of config to html2pdf:

html2canvas: {
  useCORS: true
}

Give that a try :)

@brendon not working :(

ermarkar avatar May 18 '19 06:05 ermarkar

Hi @ermarkar, sorry you've been having trouble. I suspect this is related to the way nodes are currently cloned in html2pdf.js. If so, it's something I've been meaning to fix for ages.

I've worked out a potential fix but it's not yet fully tested. Could you give it a spin and let me know if it solves your problem? It's here:

html2pdf.js/bugfix/clone-nodes-BUILD

Specifically, you can go into the /dist/ directory and pull out whichever files your project relies on.

eKoopmans avatar May 19 '19 10:05 eKoopmans

@eKoopmans Thanks for the reply, Actually the CSS rules that are written in .css files are not getting applied, so i copied directly to my html (inline styling

Yes sure, I will try the given link as well.

Thanks!!! :)

ermarkar avatar Jun 07 '19 09:06 ermarkar

Glad you got things working! That said, we definitely want external stylesheets to work properly. If you have a chance, please test the other build (with your original stylesheets) and see if it solves things - thanks!

eKoopmans avatar Jun 09 '19 06:06 eKoopmans

Hi @eKoopmans , I have external scss file and styles are not getting applied while generating pdf? Is there any way to attach '.scss' file in html2pdf.js ?

Zankhna95 avatar May 18 '20 20:05 Zankhna95

@Zankhna95, you'd need to compile this to css first at the very least I would say.

brendon avatar May 18 '20 20:05 brendon

@eKoopmans Thanks for the reply, Actually the CSS rules that are written in .css files are not getting applied, so i copied directly to my html (inline styling

Yes sure, I will try the given link as well.

Thanks!!! :) Hello Champs, I have html contents with inline styling, for me style is not getting applied to the generated pdf. I have used below options for html2pdf in my react project. const PDF_OPTIONS = { margin: [5, 5, 5, 5], pagebreak: { after: '#pageBreakers' }, image: { type: 'jpg', quality: 0.95 }, html2canvas: { scale: 4, dpi: 192, letterRendering: true, useCORS: true }, jsPDF: { unit: 'pt', format: 'A4', orientation: 'portrait', autosize : false } }; html2pdf().from(dataContainer, 'element').set(PDF_OPTIONS).to('pdf').save("TermSheet.pdf"); Would you be able to help with any pointer? I am using below dependencies versions in my react project. html2pdf.js version = 0.9.2 "es6-promise": "^4.2.5", "html2canvas": "^1.0.0-alpha.12", "jspdf": "^1.4.1"

SubhashKocher avatar Jun 10 '20 05:06 SubhashKocher

I came here cause I had a similar issue.

I use Vue as frontend framework, in case you are not familiar typically the structure in a Vue component is as follows

<template>
Here goes your HTML
</template

<script>
Your component data, methods etc
</script>

<styles>
Your CSS
</styles>

When wotking locally, and watching my changes on localhost, I could generate an html (it was dinamically generated by concat string with html code inside) and the styles would apply. But after deploying my brack to test in staging the styles were not applied.

I also solved this by adding inline styles in the html.

Posting this in case you are still looking into this issue.

juansinmiedos avatar Jul 31 '20 21:07 juansinmiedos

I just ran into this issue and happened upon a solution for my particular case.

My case is a 20+ page document generated from an app, with the CSS being compiled from Sass. The doc contains a page for each of ~22 items, with various metrics for each. The styles were working fine when I exported the parent div; once I split up each item onto its own page, I ran into this issue - #19 . I implemented the workaround explained here and here in which each page is generated via a separate canvas. However, selecting the individual items within the parent div rather than the parent div itself caused the styling to break.

What fixed it for me was unnesting the selectors in my Sass files such that the selectors targeting the divs being used for the pdf pages were not nested within any other selector. I.e. they are separate / on the first level / all the way left, etc.

Once I made that switch, the styling started working again in the PDFs.

tobias-fyi avatar Jan 27 '21 21:01 tobias-fyi

Please decrease the value of scale in html2canvas options keep scale:0.8 and try it will work

dattanaik1211 avatar May 07 '21 12:05 dattanaik1211

Please decrease the value of scale in html2canvas options keep scale:0.8 and try it will work

for me it worked. but quality is too low. Also sometime in higher scale I have got correct output.

vishnudas707 avatar May 10 '21 18:05 vishnudas707

What fixed it for me was unnesting the selectors in my Sass files such that the selectors targeting the divs being used for the pdf pages were not nested within any other selector. I.e. they are separate / on the first level / all the way left, etc.

Once I made that switch, the styling started working again in the PDFs.

Thanks @tobias-fyi, this alone worked for me. Although it was bit of work it really made me understand how SCSS makes my life a bit easier.

brunomptavares avatar Jun 18 '21 19:06 brunomptavares

I am using Angular 12 and the styling that is missing is getting incorporated into my project from a third party UI library (DevExtreme), and auto generates the css on build. Are there plans on making this work without the workarounds of moving all the css around and manually unnesting the selectors after each generation of the styling?

jakehockey10 avatar Jan 04 '22 22:01 jakehockey10

It was an easier fix for me. I wasn't seeing any styling, until I realised the CSS was loading after the script.

I put the css above the script and now the PDF is beautiful.

Thanks!

Dnncha avatar Jan 13 '22 14:01 Dnncha

Is this still an issue? I am not able to see CSS styles in my PDF. I have my styles directly in my html page within

Screen Shot 2022-06-23 at 11 45 09 AM

const options = { margin: 1, filename: 'ROI_results.pdf', image: { type: 'jpeg', quality: 0.98 }, html2canvas: { scale: 2 }, jsPDF: { unit: 'in', format: 'letter', orientation: 'portrait' } }; const savePDF = function() { html2pdf().set(options).from(element).save(); }

dbraun-ascendium avatar Jun 23 '22 16:06 dbraun-ascendium

Is this still an issue? I am not able to see CSS styles in my PDF. I have my styles directly in my html page within

Screen Shot 2022-06-23 at 11 45 09 AM

const options = { margin: 1, filename: 'ROI_results.pdf', image: { type: 'jpeg', quality: 0.98 }, html2canvas: { scale: 2 }, jsPDF: { unit: 'in', format: 'letter', orientation: 'portrait' } }; const savePDF = function() { html2pdf().set(options).from(element).save(); }

I guess it is, i also have no css here when generating pdf from vue

sicadev avatar Nov 05 '22 10:11 sicadev

I had the same issue.

Im using Vue as a fronted framework and I didn't want to use in-line styles, so I solved by doing the following :

I had this before and it didn't working:

<template>
    <div ref="toPrint" class="d-none">
        <v-card outlined flat>
            <v-simple-table >
                  ...
            </v-simple-table>
        </v-card>
    </div>
</template>

I remembered that for all classes in Vue to work as expected, they have to be inside the class .v-application, so I did this change:

<template>
    <div ref="toPrint" class="v-application d-none">
        <div class="v-application--wrap">
             <v-card outlined flat>
                  <v-simple-table >
                       ...
                   </v-simple-table>
                </v-card>
          </div>
    </div>
</template>    

Once I made that change, the Vue classes started showing in the PDF.

GadielHernandez avatar Feb 08 '23 01:02 GadielHernandez

I somehow found out that some rules applied and some didn't in my css file and the differences between them were like below (In my .css file) .viewer_bg .doc { max-width:640px; height: 920px !important; padding: 80px; margin: 60px auto; background-color:#FFF; overflow-y:auto; box-shadow: 10px 10px 5px #454545; } above one wasn't applied .doc { max-width:640px; height: 920px !important; padding: 80px; margin: 60px auto; background-color:#FFF; overflow-y:auto; box-shadow: 10px 10px 5px #454545; } this one was applied to pdf i cannot figure out how did this happen , hope this would help though.

for detailed info, i was using 0.9.3 and below code is to call html2pdf ` $('#toPdf').click(function (e) {

        console.log("topdf");

        let element = document.getElementById("pdf_wrap");

        var opt = {
            margin: 10,
            filename: 'myfile.pdf',
            image: { type: 'png', quality: 1 },
            html2canvas: {
                scale: 1,
                ignoreElements: function (el) {
                    return el.classList.contains("noPdf");
                }
            },
            jsPDF: { unit: 'mm', format: 'a4', orientation: 'portrait' },                
        };

        html2pdf().set(opt).from(element).save();
    });

`

oneofU avatar Jun 23 '23 06:06 oneofU