从javascript/cordova编写pdf

从javascript/cordova编写pdf,javascript,cordova,pdf,stream,Javascript,Cordova,Pdf,Stream,在我们的移动应用程序(cordova+html4)中,我们需要显示来自流的PDF。我们有一个返回pdf流的服务。我们希望将该流存储到手机的临时文件夹位置,并显示PDF。 下面的示例java代码正是我所需要的。但是如何在java脚本上实现这一功能呢?我的意思是用java脚本读取二进制流 String fileURL = "https://1/////4/xyz"; String saveDir = "D:/Works"; try { URL url = new URL(fileURL);

在我们的移动应用程序(cordova+html4)中,我们需要显示来自流的PDF。我们有一个返回pdf流的服务。我们希望将该流存储到手机的临时文件夹位置,并显示PDF。 下面的示例java代码正是我所需要的。但是如何在java脚本上实现这一功能呢?我的意思是用java脚本读取二进制流

String fileURL = "https://1/////4/xyz";
String saveDir = "D:/Works";
try {
    URL url = new URL(fileURL);
    HttpURLConnection httpConn = (HttpURLConnection) url
            .openConnection();
    httpConn.setRequestProperty("Content-Type",
            "application/x-www-form-urlencoded");
    httpConn.setRequestProperty("TOKEN",
            "ghZtxnPfpJ63FgdT/59V+5zFTKHRdwm6rIfGJC+0B5W5CJ9pG33od7l+/L6S8R56");
    int responseCode = httpConn.getResponseCode();
    System.out.println("Reseponse Code = " + responseCode);

    if (responseCode == HttpURLConnection.HTTP_OK) {
        String fileName = "";
        String disposition = httpConn
                .getHeaderField("Content-Disposition");
        String contentType = httpConn.getContentType();
        int contentLength = httpConn.getContentLength();

        if (disposition != null) {
            // extracts file name from header field
            int index = disposition.indexOf("filename=");
            if (index > 0) {
                fileName = disposition.substring(index + 10,
                        disposition.length() - 1);
            }
        } else {
            // extracts file name from URL
            fileName = fileURL.substring(fileURL.lastIndexOf("/") + 1,
                    fileURL.length());
        }

        System.out.println("Content-Type = " + contentType);
        System.out.println("Content-Disposition = " + disposition);
        System.out.println("Content-Length = " + contentLength);
        System.out.println("fileName = " + fileName);

        // opens input stream from the HTTP connection
        InputStream inputStream = httpConn.getInputStream();
        String saveFilePath = saveDir + File.separator + fileName;

        // opens an output stream to save into file
        FileOutputStream outputStream = new FileOutputStream(
                saveFilePath);

        int bytesRead = -1;
        byte[] buffer = new byte[BUFFER_SIZE];
        while ((bytesRead = inputStream.read(buffer)) != -1) {
            outputStream.write(buffer, 0, bytesRead);
        }

        outputStream.close();
        inputStream.close();

        System.out.println("File downloaded");
    } else {
        System.out
                .println("No file to download. Server replied HTTP code: "
                        + responseCode);
    }
    httpConn.disconnect();
} catch (MalformedURLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

您可能需要查看以下两个库:

如果您试图从javascript和

如果您试图在界面中实现pdf查看器

这两个人中的一个应该能帮你