Git Product home page Git Product logo

Comments (19)

marcbachmann avatar marcbachmann commented on August 17, 2024 6

I believe it should work. Doesn't it?

<link rel="stylesheet" type="text/css" href="file:///absolute/path/to/css/file.css">
<link rel="stylesheet" type="text/css" href="http://example.com/assets/stylesheet.css">

I'll move that into the readme.
I'm curious what you want to use. Did you try to link local or remote files?

from node-html-pdf.

negi-sachin avatar negi-sachin commented on August 17, 2024 4

I solved this by passing "base" key in options:
Eg:
Putting all static files inside "public" folder and then in app.js:
app.use(express.static("public"));

let options = {
base: "http://localhost:3000", // or use: req.protocol + '://' + req.get('host')
};
pdf.create(html, options).toFile("report.pdf", function (err, data) {
if (err) {
res.send(err);
} else {
res.send("File created successfully");
}
});
}

I can access my static files like :

<script src="js/script.js" type="text/javascript"></script>

from node-html-pdf.

itajaja avatar itajaja commented on August 17, 2024 1

you can use the <base> html tag. works for me :)

from node-html-pdf.

ravisuhag avatar ravisuhag commented on August 17, 2024

Hi... Thanks for response....!
No It is not working. i tried both

Also for finished version i am generating dynamic HTML with HAML template engine so right now in-paging all CSS.... Can't figure out how i can use external CSS file and avoid duplicating the CSS code in every template ...

from node-html-pdf.

marcbachmann avatar marcbachmann commented on August 17, 2024

This works as long as you use the stylesheets in the html string.
pdf.create(html, options, callback)

from node-html-pdf.

marcbachmann avatar marcbachmann commented on August 17, 2024

Have a look at the examples at here

from node-html-pdf.

istian avatar istian commented on August 17, 2024

@marcbachmann,

I also followed the example but something weird going on my testing. I have two enviroments QA and prod, the pdf creation works well in QA but failing in production. My CSS file is minified in production but if I try and replace QA with the production minified css version, it still worked in QA.

I suspect that it maybe a delay in fetching the external css (via absolute url) and the html page that the pdf creator did not get the full render of html content with css applied to it. But why does it work in my QA server (same html)?

I'm thinking that this maybe on my production server but I couldn't tell where. Is there a debug mode option for the npm package so I can see more details? Can I use phantomjs to see logs or debug?

Thanks

from node-html-pdf.

lfilho avatar lfilho commented on August 17, 2024

Hi, this is not working for me too.

    <link rel='stylesheet' type='text/css' href='lib/foundation-5.5.1/css/normalize.css' />
    <link rel='stylesheet' type='text/css' href='css/font-awesome.min.css' />
    <link rel='stylesheet' type='text/css' href='css/main.css' />
    <link rel='stylesheet' type='text/css' href='css/print.css' />

and:

#!/usr/bin/env node
'use strict';

var fs = require('fs');
var pdf = require('html-pdf');
var html = fs.readFileSync('./index.html', 'utf8');
var options = { filename: './CV_-_Luiz_Gonzaga_Filho.pdf', format: 'A4' };

pdf.create(html, options).toFile(function(err, res) {
    if (err) { return console.log(err); }
    console.log(res);
});

The pdf is created but with no CSS at all, just the RAW html content.

from node-html-pdf.

inkub8 avatar inkub8 commented on August 17, 2024

@lfilho - I can't tell you the definitive reason, though I suspect it has to do with the fact it's using the headless browser to render your pdf, but you need to use absolute links. So, for example:

<link rel='stylesheet' type='text/css' href='http://www.mysiteurl.com/css/main.css' />

That should fix your issue.

from node-html-pdf.

istian avatar istian commented on August 17, 2024

i had an issue before and i solved it by specifying the full file path of the css from te server. Absolute url didn't work for me.

from node-html-pdf.

lfilho avatar lfilho commented on August 17, 2024

Thanks, @inkub8. Will try that.

That is a shame. My use case is a static page (my cv, done in html + css). So now I would have to:

  1. Have a local server as dependency of my project
  2. Create a build pipeline, something like:
    1. Prefix my hrefs with http://localhost:8080/
    2. Build the PDF
    3. Revert i.

The 1. wouldn't be much of problem because I'm actually using a local server for live-reloading changes. But of course I don't want to commit files with that href. :)

from node-html-pdf.

inkub8 avatar inkub8 commented on August 17, 2024

@lfilho I use
req.protocol + '://' + req.get('host')

this stops me having to commit fixed hrefs.

from node-html-pdf.

lfilho avatar lfilho commented on August 17, 2024

Ha, well thought! Thanks!

from node-html-pdf.

KylePDavis avatar KylePDavis commented on August 17, 2024

Thanks @itajaja, using <base> worked for me.

from node-html-pdf.

marcbachmann avatar marcbachmann commented on August 17, 2024

Now in v2.0.0, there's also a base option, which might help.

from node-html-pdf.

Rahulkr204 avatar Rahulkr204 commented on August 17, 2024

I faced same problem and resolved by creating a server in parent directory and referring css url to base.

from node-html-pdf.

LuisdelaVega avatar LuisdelaVega commented on August 17, 2024

What if I need to use local files? The solution proposed here (using the <base> tag) doesn't help in that scenario

from node-html-pdf.

vaske avatar vaske commented on August 17, 2024

When I tried to include external URL for css I keep getting error select: Invalid argument, without include of CSS it generate pdf.

from node-html-pdf.

voigtd avatar voigtd commented on August 17, 2024

I can confirm that I am also intermittently getting the error select: Invalid argument. I'm loading external google fonts stylesheets, which I found is causing the problem. Loading local stylesheets works. The issue is caused by PhantomJS which is the main dependency of this project. I don't think there is any way to fix this besides building your own HTML to PDF lib with something besides Phantom

from node-html-pdf.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.