navjotdhanawat / dynamic-html-pdf

Create dynamic PDF from HTML template using handlebars
MIT License
34 stars 10 forks source link

Image url in CSS #2

Open garcia-marco opened 6 years ago

garcia-marco commented 6 years ago

Hello, i need to put a background image on each page of my template so i'm doing :

<div class="page" style="background-image: url('{{url}}/mypath/background.jpg'); background-size: cover;">

{{url}} is the url of my website. And it's not working but same path is working for the image : <img src="{{url}}/mypath/img.jpg">

Do you know how i can fix it ?

(I already try relative path or adding !important)

navjotdhanawat commented 6 years ago

@garciamarco33 The image url path you are accessing on client is accessible because you have serve it from server to client. You should have to give image relative path from server root and not by website URL. For ex:

-server.js
-images
  --image.jpg
template.html

In this scenario, we can use image path :'/images/image.jpg'

garcia-marco commented 6 years ago

Told you relative path won't work,

-template --pdf.html

-public --assets ---img ----img.jpg

with this I'm doing /public/assets/img/img.jpg and it doesn't work. But I find a solution for this, I put an <img> with

img {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
}

Now I'm blocking with dynamic image in {{#each}}. I have :

{{#each array}}
  <div>
    <img src="{{url}}/assets/img/{{this.id}}.jpg">
    <h5>{{this.name}}</h5>
  </div>
{{/each}}

{{this.name}} is working but image don't Relative path doesn't work here neither I already try Do you know something about this ?

navjotdhanawat commented 6 years ago

Sorry, I haven't tried this scenario.