Comments (19)
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.
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.
you can use the <base>
html tag. works for me :)
from node-html-pdf.
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.
This works as long as you use the stylesheets in the html string.
pdf.create(html, options, callback)
from node-html-pdf.
Have a look at the examples at here
from node-html-pdf.
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.
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.
@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.
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.
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:
- Have a local server as dependency of my project
- Create a build pipeline, something like:
- Prefix my
href
s withhttp://localhost:8080/
- Build the PDF
- Revert
i.
- Prefix my
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.
@lfilho I use
req.protocol + '://' + req.get('host')
this stops me having to commit fixed hrefs.
from node-html-pdf.
Ha, well thought! Thanks!
from node-html-pdf.
Thanks @itajaja, using <base>
worked for me.
from node-html-pdf.
Now in v2.0.0, there's also a base
option, which might help.
from node-html-pdf.
I faced same problem and resolved by creating a server in parent directory and referring css url to base.
from node-html-pdf.
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.
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.
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)
- Background color not applying those area where no html
- Image not showing when uploaded on vps server
- QR code not visible over generated pdf post ami upgrade HOT 2
- Fonts don't work properly on server HOT 4
- stream (.toStream) not generated after deploying to k8s HOT 2
- The download pdf always have an empty page at last HOT 1
- html-pdf: Unknown Error throw new ERR_INVALID_ARG_TYPE(name, 'string', value); Docker HOT 1
- Pdf saved in TMP directory HOT 1
- Is watermark possible in pdf? HOT 3
- Not able to resize the chebox size while generating the pdf
- Syntax for forcing a page break HOT 2
- Potential injection vulnerability in node-html-pdf
- Page overflow with white line
- PDF Creation Error: spawn /root/pando-app/node_modules/phantomjs-prebuilt/lib/phantom/bin/phantomjs ENOENT HOT 1
- The answer to how to resolve Windows error of phantomjs ENOENT issue
- Error: spawn E2BIG
- error HOT 2
- Error: spawn C:\home\site\wwwroot\node_modules\phantomjs-prebuilt\lib\phantom\bin\phantomjs ENOENTat
- This package has been deprecated HOT 1
- Some Images from urls are not rendered HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from node-html-pdf.