I am using html-pdf( v2.1.0) to convert and generate report in pdf format. I tried to have common header in all the pages using header option provide in the plugin by setting the base path like below.
var base = 'file://' + __dirname + '/logos/';
Printing base path
file:///Users/Documents/Sample Project/Code/Web Services/api/logos/
"header": {
"height": "25mm",
// "contents": "<img height='100' width='100' src='ACL.png'>"
"contents": "<P ALIGN=CENTER STYLE='margin-top: 0.04in; margin-bottom: 0.46in'><B>ACL Report</B><IMG SRC='SUN.png' ALIGN=LEFT HSPACE=12 WIDTH=113 HEIGHT=41 BORDER=0><IMG SRC='AAL.png' ALIGN=RIGHT HSPACE=12 WIDTH=135 HEIGHT=33 BORDER=0></P>"
}
Please help if anyone have faced the same issue.
You must also add the image in your html body like below:
<html>
<body>
<img src="SUN.png"/>
... other content
</body>
</html>
I need add 2 image tags in body (should be display:none;):
<img
src="img/logo.png"
and in header:
<img
src="file:///C:/Users/..../logo.png"
also make sure the base:
base:file:///${path.join(root.get(), 'assets/').replace(/\/g, '/')}``
it is works in "html-pdf": "^2.2.0",
I am using same format but this code is not working
why is use of root base: file:///${path.join(root.get(), 'assets/').replace(//g, '/')}``
Hi All,
I am trying to show image in header but it keeps on showing blank space. I tried using options as well as
div id="pageHeader" style = "height:75px"
img src="http:example.com/abc/aaa"
I even tried to convert image to base64 and set as
div id="pageHeader" style = "height:75px"
img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAA...."
Please help me ASAP.
Issue created : https://github.com/marcbachmann/node-html-pdf/issues/561
I was able to get it working doing the following:
const cwd = process.cwd();
const base = `file://${cwd}/public`;
const cwd = process.cwd();
const base = `file://${cwd}/public`;
const options = {
format: 'Legal',
orientation: 'portrait',
border: '10mm',
width: '215.9mm',
height: '355.6mm',
footer: {
height: '10mm',
},
type: 'pdf',
timeout: 30000,
header: {
height: '25mm',
contents: `
<div style="text-align: center;">
<img src="${base}/my-image.png" width="200" height"100"/>
</div>`,
},
};
`<!DOCTYPE html>
<html>
<head>
<title>My page</title>
<meta charSet="utf8" />
<meta name="viewport" content="minimum-scale=1, initial-scale=1, width=device-width" />
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700&display=swap" />
</head>
<body>
<img style="display: none;" src="${base}/no-avatar.png" width="200" height"100"/>
<div id="root">${html}</div>
</body>
</html>`
It's important that both img tags have the absolute file path. Or else it did not work for me. I used process.cwd() since the absolute paths will be different between my local build and the server I deploy my work to (two different environments, therefore different locations). So it was important to get that path dynamically.
Most helpful comment
I was able to get it working doing the following:
It's important that both img tags have the absolute file path. Or else it did not work for me. I used
process.cwd()since the absolute paths will be different between my local build and the server I deploy my work to (two different environments, therefore different locations). So it was important to get that path dynamically.