cordova-print-pdf-plugin icon indicating copy to clipboard operation
cordova-print-pdf-plugin copied to clipboard

printer show, but it keep loading preview

Open nguyenhuuloc304 opened this issue 8 years ago • 6 comments

As title, ios show a printer pop up, but the content is keep loading preview. firstly i try to use URI but not work. then i change the way to use base64. from file pdf on local. i use cordovafile.readAsDataURL to convert file pdf to base64. but it still not work. please help me.

below is my code. could you take a look at it success is returned base64string $cordovaFile.readAsDataURL(pathDirectory, fileName) .then(function (success) { window.plugins.PrintPDF.isPrintingAvailable( function(isAvailable) {

                    alert('printing is success: '+ success.substring(0,200));                        
                    if(isAvailable){                         
                        window.plugins.PrintPDF.print({
                            data: success,
                            type: 'Data',
                            title: fileName,
                            success: function(){
                                alert('success');
                            },
                            error: function(data){
                                data = JSON.parse(data);
                                alert('failed: ' + data.error);
                            }
                        });
                    }
                });  
                //alert(JSON.stringify(success));
            }, function (error) {
                alert(JSON.stringify(error));
            });

nguyenhuuloc304 avatar Aug 21 '16 14:08 nguyenhuuloc304

I'm experiencing the same issue, any update on this?

john-doherty avatar Feb 23 '17 13:02 john-doherty

Hello,

I also am experiencing this same issue. I have tried to use this code with type: 'File' and the data is a URL to a pdf that I am storing in my expansion file. The printer preview opens then my app crashes. Is this plugin compatible with this cordova plugin? -- > https://github.com/agamemnus/cordova-plugin-xapkreader#cordova-5.0

If you could provide an example of how to open a pdf from an expansion file and convert it to a base64 string that would be extremely helpful, because I can't get that to work either.

jbaartman avatar Feb 23 '17 17:02 jbaartman

I got it working. Turned out my data was not correctly base64 encoded

john-doherty avatar Feb 23 '17 17:02 john-doherty

How did you get it properly base64 encoded? Were you using a local pdf file or a pdf hosted on a server? I am trying to figure out how to encode a pdf file located in my expansion file with no success.

jbaartman avatar Feb 23 '17 17:02 jbaartman

My app is taking to a nodejs REST API, so I just modified the API to return the PDF content as a Base64 string and it worked.

If you're trying to do this client side, you might be able to use FileReader.readAsDataURL(). Just remember to strip the data:application/pdf;base64, part of the output string before passing it to the plugin as suggested in the docs

john-doherty avatar Feb 23 '17 17:02 john-doherty

Yes, I have tried to do this client side but can't seem to get the PDF to open or be served locally. I have tried the below code var files = new File([""], "filename.pdf",{type:'application/pdf'}); console.log(files); if (files.length > 0) { getBase64(files[0]); } });

function getBase64(file) { var reader = new FileReader(); reader.readAsDataURL(file); reader.onload = function () { console.log(reader.result); }; reader.onerror = function (error) { console.log('Error: ', error); }; }

where filename.pdf is a pdf stored in my expansion file and it does not read it in as a proper array/file type. I keep getting file size 0...

jbaartman avatar Feb 23 '17 17:02 jbaartman