我正在一个应用程序中工作,我需要根据从服务器获得的响应创建 pdf 文件。有什么方法可以使用此响应创建 pdf 吗?响应如下所示:
%PDF-1.4
%����
2 0 obj
<</Type/XObject/Subtype/Image/Width 422/Height 119/Length 72/ColorSpace/DeviceGray/BitsPerComponent 8/Filter/FlateDecode>>stream
x���
��o7����[qD
endstream
endobj
3 0 obj....
.......
以下是我用来将其转换为 pdf 的方法,但我收到 BAD-BASE64 错误
private void convertBase64StringToPdfAndStoreIt(String base64PDf) throws IOException {
final int notificationId = 1;
String currentDateTime = DateFormat.getDateTimeInstance().format(new Date());
final File dwldsPath = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DOWNLOADS) + "/FileName_" + currentDateTime + "_.pdf");
byte[] pdfAsBytes = Base64.decode(base64PDf.replaceFirst("application/pdf", ""), 0);
FileOutputStream os;
os = new FileOutputStream(dwldsPath, false);
os.write(pdfAsBytes);
os.flush();
if(dwldsPath.exists()) {
NotificationCompat.Builder b = new NotificationCompat.Builder(InspectionformWebView.this, "MY_DL")
.setDefaults(NotificationCompat.DEFAULT_ALL)
.setWhen(System.currentTimeMillis())
.setContentTitle("MY TITLE")
.setContentText("MY TEXT CONTENT");
nm = (NotificationManager) InspectionformWebView.this.getSystemService(Context.NOTIFICATION_SERVICE);
if(nm != null) {
nm.notify(notificationId, b.build());
Handler h = new Handler();
long delayInMilliseconds = 5000;
h.postDelayed(new Runnable() {
public void run() {
nm.cancel(notificationId);
}
}, delayInMilliseconds);
}
}
}