Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

open() not working on Android #679

Closed
girish-fuluskar opened this issue Aug 25, 2016 · 16 comments
Closed

open() not working on Android #679

girish-fuluskar opened this issue Aug 25, 2016 · 16 comments

Comments

@girish-fuluskar
Copy link

Hi,

I am using PDFMake in my assignment, its working as expected (on android device and on laptop) and like to thank you for this.
However, Download() method not working on android devices.
My requirement is, after I show the PDF, pdfMake.createPdf(documentDefination).open(), I want to download same pdf to my android device and for this I am using pdfMake.createPdf(documentDefination).downlaod("test.pdf"). Its working fine on chrome but not on physical android device.
Does it save the PDF some other location on device or how I can achieve this?

Girish D. Fuluskar

@liborm85 liborm85 changed the title Download() not working Download() not working on Android Dec 23, 2016
@liborm85 liborm85 changed the title Download() not working on Android download() and open() not working on Android Dec 29, 2016
@liborm85
Copy link
Collaborator

reference: #800

@liborm85 liborm85 added the bug label Dec 29, 2016
@anmolagar
Copy link

Hi Guys, any updates on this, can we expect a fix here soon for android and ios?

@niravparsana94
Copy link

Any updates?

@prabodh25
Copy link

any updates on this? I am also facing same issue. where does file gets saved on the device after calling download and how to get the complete path?

@francobasilico
Copy link

francobasilico commented Jul 2, 2017

any news on this, downloading is not working on android devices, and also opening, opens a new blank tab

@marcomessa
Copy link

I found a workaround, and it's working seamlessly both on android and ios.

const doc = pdfMake.createPdf(docDefinition); doc.getBase64((data) => { window.location.href = 'data:application/pdf;base64,' + data; });

Hope this helps

@uebbi
Copy link

uebbi commented Jul 31, 2017

@marcomessa You helped me a lot! Tks!

@marcomessa
Copy link

marcomessa commented Aug 1, 2017

You are welcome!

Be aware that this solution is gonna be deprecated in future Chrome versions!
But for now is a nice workaround!

@neerajnarayanan
Copy link

@marcomessa Followd the steps you have specified.But showing this in the console.

Resource interpreted as Document but transferred with MIME type application/pdf

Also No new window is opening in chrome.Can u suggest any solutions

@vytautas-pranskunas-
Copy link

Any update on this?
For some reason marcomessa workaround is not working neithe ron chrome neither on mobile device.

@liborm85
Copy link
Collaborator

Chrome on Android throws error after download pdf file - "download failed due to unknown error":

  • Chrome on Android 6.x, works without error
  • Chrome on Andorid 7.x, throw error, fixed in Chrome version 60+ (actual version is 63)

=> In actual Chrome versions works download() correctly in Android 6 and 7.

// edited issue title

@liborm85 liborm85 changed the title download() and open() not working on Android open() not working on Android Dec 25, 2017
@zur1eel
Copy link

zur1eel commented May 2, 2018

someone find a solution? I'm building a cordova app and .download() don't work on devices.

@niravparsana94
Copy link

niravparsana94 commented May 2, 2018

Below code(Ionic) is works for me, tested on Android/iOS/Browsers.
[NOTE: I'm using file opener plugin to open PDF.]

if (this.platform.is('cordova')) {
	// FOR MOBILE DEVICES
	pdfmake.createPdf(newdd).getBuffer((buffer) => {
	  var utf8 = new Uint8Array(buffer); // Convert to UTF-8...
	  let binaryArray = utf8.buffer; // Convert to Binary...

	  let dirPath = "";
	  if (this.platform.is('android')) {
		dirPath = this.file.externalRootDirectory;
	  } else if (this.platform.is('ios')) {
		dirPath = this.file.documentsDirectory;
	  }

	  let dirName = 'DailySheet';

	  this.file.createDir(dirPath, dirName, true).then((dirEntry) => {
		let saveDir = dirPath + '/' + dirName + '/';
		this.file.createFile(saveDir, fileName, true).then((fileEntry) => {
		  fileEntry.createWriter((fileWriter) => {
			fileWriter.onwriteend = () => {
			  this.hideLoading();
			  this.showReportAlert('Report downloaded', saveDir + fileName);
			  this.fileOpener.open(saveDir + fileName, 'application/pdf')
				  .then(() => console.log('File is opened'))
				  .catch(e => console.log('Error openening file', e));
			};
			fileWriter.onerror = (e) => {
			  this.hideLoading();
			  this.showAlert('Cannot write report', e.toString());
			};
			fileWriter.write(binaryArray);
		  });
		}).catch((error) => { this.hideLoading(); this.showAlert('Cannot create file', error); });
	  }).catch((error) => { this.hideLoading(); this.showAlert('Cannot create folder', error); });
	}).catch((error) => { this.hideLoading(); this.showAlert('Error while creating pdf', error); });
  }
  else {
	//FOR BROWSERS
	pdfmake.createPdf(newdd).download(fileName);
	this.hideLoading();
  }

@zaraemilien
Copy link

@niravparsana94 You helped me a lot! Thanks

@niravparsana94
Copy link

@niravparsana94 You helped me a lot! Thanks

I'm glad. :)

@liborm85
Copy link
Collaborator

Android not support open pdf in browser, but open invokes dialog to open document.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests