function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Maguy Fogaing 2Maguy Fogaing 2 

change my blob url to data url

Hello everyone, 
l used the librairy Jspdf to download my html as a pdf in LWC component as below : 
const {jsPDF} = window.jspdf;
      let doc = new jsPDF('p', 'pt', 'A4');
      doc.addFileToVFS('Muli-normal.ttf', helper.getFontCode());
      doc.addFont('Muli-normal.ttf', 'Muli', 'bold');
      doc.setFont('Muli','bold');
      doc.text(210,25,'titleofdocument');
     
      doc.autoTable({
        margin: {top: 65},
        styles: {font: "Muli"},
        headStyles:{fillColor:'#E30613'},
        head: [['title1', 'title2', 'title3','title4', 'title5']],
        body: this.allelements,
      }).save('titleofdocument.pdf');
  
The problem is when l download the pdf it's give me an url as Blob://htpps:Name_of_the_site which cause me some problems in my application mobile version. So please l need help to change this url Blob as data url like this data:application/pdf;base64.
Please propose me other ways to download my html as a pdf in LWC so that l can obtain a data url instead of a blob url.
It's urgent l am struggling with this for a week now.

Thank you in advance.
Best Answer chosen by Maguy Fogaing 2
Piyush jadavPiyush jadav

Hi

You can use doc.output('datauristring') or doc.output('datauri') instead of doc.save.

All Answers

Piyush jadavPiyush jadav

Hi

You can use doc.output('datauristring') or doc.output('datauri') instead of doc.save.

This was selected as the best answer
Maguy Fogaing 2Maguy Fogaing 2
Hello Piyush jadav,
Your answer helps me to get the data url, but l am not able to download the pdf. l use your answer like this :
doc.autoTable({
        margin: {top: 65},
        styles: {font: "Muli"},
        headStyles:{fillColor:'#E30613'},
        head: [['title1', 'title2', 'title3','title4', 'title5']],
        body: this.allelements,
      }).output('datauristring');

But still don't download the pdf. Please can you propose me any solution to download pdf with doc.output('datauristring') ?

Thank you in advance.
Piyush jadavPiyush jadav

Hi

can you try this code

doc.output('dataurlnewwindow');

mark this as best answer is it's help you.

Thanks

Maguy Fogaing 2Maguy Fogaing 2
With doc.output('dataurlnewwindow') l am still not able to download. It's open a new windows but with empty content with an error. l join the image User-added image
Please needs helps to fix this issue
Piyush jadavPiyush jadav

Try this

 

window.open(URL.createObjectURL(doc.output("blob")))

Maguy Fogaing 2Maguy Fogaing 2
I tried window.open(URL.createObjectURL(doc.output("blob"))) but it shows me what l dont want an url Blob instead of a data url. By using doc.output('datauristring') l was able to obtain a data url but my problem is l am not able to download the pdf. 
Piyush jadavPiyush jadav

var string = doc.output('datauristring');

var x = window.open();

x.document.open();

x.document.location=string;

Maguy Fogaing 2Maguy Fogaing 2
Thank you it helps me.