10

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

Rubén
  • 34,714
  • 9
  • 70
  • 166
proppy
  • 10,495
  • 5
  • 37
  • 66
  • 1
    What did you try? Did you search? Did you read the API documentation? – Jocelyn Oct 11 '12 at 16:11
  • 1
    @Jocelyn Proppy works at Google, so I'm going to take a wild guess and say he probably tried the obvious stuff before posting. – Nick Johnson Oct 11 '12 at 16:14
  • @NickJohnson Well, we are not wizards, how are we supposed to guess that? Everyone posting a question on Stackoverflow is expected to show what he tried, what he already searched for. – Jocelyn Oct 11 '12 at 16:17
  • @Jocelyn that was not obvious from the documentation https://developers.google.com/drive/integrate-open#open_and_convert_google_docs_in_your_app that this could be done without any user interaction. I updated my question and will remember to state the research I already made for future ones. – proppy Oct 11 '12 at 16:18
  • @Jocelyn One option would be to click on his name and read his profile description, which states "App Engine Developer Programs Engineer, based in Zurich, part of Developer Relations team at Google". – Nick Johnson Oct 12 '12 at 13:05
  • @Nick Indeed, this info was in the OP's profile. But you can't expect everyone to read the OP's profile before answering the OP's question. All the important information must be directly available in the question. – Jocelyn Oct 12 '12 at 13:49

2 Answers2

11

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

Strom
  • 356
  • 2
  • 6
Ali Afshar
  • 40,967
  • 12
  • 95
  • 109
2

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();
}
David
  • 1,510
  • 20
  • 16