I am writing some content in bullet points in HTML and then using swiper.js library ( https://swiperjs.com/demos/255-effect-cards/core) to display them as sliders. I am giving a download button to the slider and want the user to download all those sliders at once which are written inside HTML divs
Can you help me with how can make it work?
- User sees slider
- The user clicks on the download
- It should convert all N slides into image format files(png/jpeg) and
download each image to the user’s device.
code for js slider which has multiple divs and content inside each div
<div class="swiper mySwiper" >
<div class="swiper-wrapper">
<div class="swiper-slide">
<h3 #screen>{{summary[0]}}</h3>
</div>
<div class="swiper-slide" >
<h3 #screen>{{summary[1]}}</h3>
</div>
<div class="swiper-slide">
<h3>{{summary[2]}}</h3>
</div>
<div class="swiper-slide">
<h3>{{summary[3]}}</h3>
</div>
<div *ngIf="summary[4] != null" class="swiper-slide">
<h3>{{summary[4]}}</h3>
</div>
</div>
</div>
converting each div into an image
<div id="download" class="main" *ngIf="image">
<img #canvas>
<a #downloadLink></a>
</div>
@ViewChild('screen') screen: ElementRef;
@ViewChild('canvas') canvas: ElementRef;
@ViewChild('downloadLink') downloadLink: ElementRef;
when user clicks on the button it should download all of the slides in one go
html2canvas(this.screen.nativeElement).then((canvas: any) => {
this.canvas.nativeElement.src = canvas.toDataURL();
this.downloadLink.nativeElement.href = canvas.toDataURL('image/png');
this.downloadLink.nativeElement.download = 'summary.png';
this.downloadLink.nativeElement.click();
});
2
Answers
I’ve used this library before, but it only works for saving images written to the canvas. documentação da biblioteca. eligrey/FileSaver
HTML
div class="swiper-slide"><img src=’pic_the_scream.jpg’
JAVASCRIPT
};
meustestes: This is the github repository I used for testing. To see the code working, see the repository on github that let.
There may be cross contamination issues. The code gives error when I try to run inside the machine.
The images need to be inside the origin server, but if you try to apply the code below, the code saves an empty image.
But if I run the code inside w3schools , the image origin server , the image is downloaded intact.
Good luck resolving this issue.
As already commented by Anant V jszip.js can combine multiple files to a single download.
The slightly tricky parts:
The download functionality doesn’t work in SO snippets:
See working codepen example
How it works: