简体   繁体   English

使用Phonegap将base64字符串作为pdf写入电话存储

[英]Write base64 string as pdf to phone storage using Phonegap

I have requirement to write pdf to phone storage using Phonegap , but phonegap only supports text writting in file. 我需要使用Phonegap将pdf写入电话存储,但是phonegap仅支持文件中的文本编写 I have base64 string which contains pdf doc, and I want to save it to Phonestorage . 我有base64字符串,其中包含pdf doc,我想将其保存到Phonestorage I have tried to convert base64 to utf-8 using atob function and then write it to pdf, but it is not also working. 我尝试使用atob函数将base64转换为utf-8 ,然后将其写入pdf,但它也不起作用。

Is there something else that I should try? 还有什么我应该尝试的吗? or is there any plugin available? 还是有可用的插件?

Any help would be appreciated. 任何帮助,将不胜感激。

Create a normal pdf writer method in java. 在Java中创建一个普通的pdf writer方法。 For the same iText or any other pdf writers can be used. 对于相同的iText或任何其他pdf编写器,都可以使用。 Expose a public method in this writer class as a Javascript Interface. 在此writer类中将一个公共方法公开为Javascript接口。 Eg : 例如:

Class PdfWriter{

@JavascriptInterface
    public void writeToPdf(String dataToBeWritten) {
        writeToFile(dataToBeWritten);
    }
}

Register this as a JavascriptInterface with the webview. 使用webview将其注册为JavascriptInterface

Once the webview is loaded, you should be able to call: 加载Web视图后,您应该可以调用:

window.PdfWriter.writeToPdf("data_to_be_written")

The base64 PDF data needs to be converted to a fileblob. base64 PDF数据需要转换为fileblob。

You can then save that fileblob as a PDF file using the cordova-file plugin. 然后,您可以使用cordova-file插件将该fileblob保存为PDF文件。

Convert base64 string as pdf in phonegap 在phonegap中将base64字符串转换为pdf

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM