【问题标题】:writing pdf from javascript/cordova从 javascript/cordova 编写 pdf
【发布时间】:2015-01-13 13:43:45
【问题描述】:

在我们的移动应用程序 (cordova+html4) 中,我们需要从流中显示 PDF。我们有一个返回 pdf 流的服务。我们想将该流存储到移动设备的临时文件夹位置并显示 PDF。 下面的示例 java 示例 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 cordova pdf stream


    【解决方案1】:

    您可能想看看这两个库:

    jsPDF 如果您尝试从 javascript 生成 pdf 并且

    pdf.js 如果您尝试在界面中实现 pdf 查看器。

    这两个中的一个应该可以为您解决问题。

    【讨论】:

      猜你喜欢
      • 2016-04-19
      • 1970-01-01
      • 1970-01-01
      • 2016-09-11
      • 2013-08-02
      • 2016-04-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多