如何从服务器响应创建pdf文件?

时间:2018-10-09 10:36:18

标签: android androidpdfviewer

我在一个应用程序中工作,我需要根据从服务器获取的响应来创建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);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

尝试

public String downloadPdf() {
String responseServer = "";

try {

    //////PDF folder and file init///////////////////////////

    HttpClient client = HttpClientCustomized.getHttpsClient(new DefaultHttpClient());
   //you can use the default Http Client class instead

    HttpGet get = new HttpGet(sUrl);
    get.addHeader("x-wsse", header);
    HttpResponse response = client.execute(get);
    responseCode = response.getStatusLine().getStatusCode();
    Log.d("responseCode", responseCode + "");
    HttpEntity httpEntity = response.getEntity();
    is = httpEntity.getContent();

    try {
        String typeS = "someFileName" + ".pdf";
        directory = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/SomeFolderName/");


        if (!directory.exists()) {
            directory.mkdir();
        }

        FileOutputStream fi = new FileOutputStream(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/ShippingOrders/" + typeS);

        byte[] buf = new byte[8 * 1024];
        int len = 0;
        while ((len = is.read(buf)) > 0) {
            fi.write(buf, 0, len);
        }

        fi.close();
        is.close();

        System.out.println("Hurra!! : D");
    } catch (IOException e) {
        System.out.println("owww : (" + e.toString());
    }

    responseServer = "TRUE";



} catch (IOException e) {
    Log.d("IOException", e.toString());
    return "Problems with the server";
}

return responseServer;
}