HTML2PDF using Google Drive API

后端 未结 2 1086
庸人自扰
庸人自扰 2020-12-13 16:39

Is it possible to upload and convert an HTML file to PDF using Google Drive API without user interaction?

相关标签:
2条回答
  • 2020-12-13 16:50

    Yes, it is, with two requests. You can import the file as a Google Docs, then export it to PDF. Using the Drive API.

    https://developers.google.com/drive/v2/reference/files/insert https://developers.google.com/drive/v2/reference/files/get

    0 讨论(0)
  • 2020-12-13 17:05

    worked for me (Drive docs only...)

    ByteArrayContent mediaContent = new ByteArrayContent("text/html", "HTML PAGE HERE".getBytes());
    
    File body = new File();
    body.setTitle("test.html");
    body.setMimeType("text/html");
    
    Insert request = null;
    try
    {
       request = service.files().insert(body, mediaContent);
       request.setConvert(true);
       File file = request.execute();
    
       HttpResponse resp = service.getRequestFactory().buildGetRequest(new    GenericUrl(file.getExportLinks().get("application/pdf"))).execute();
    
       OutputStream out = new FileOutputStream(getExternalFilesDir(null).getAbsolutePath() + "/test.pdf");
       byte[] buf = new byte[1024];
       int len;
       while ((len = resp.getContent().read(buf)) > 0)
        {
            out.write(buf, 0, len);
        }
        out.close();
    
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }
    
    0 讨论(0)
提交回复
热议问题