HTML2PDF using Google Drive API

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

6

2 Answers

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

1

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(); } 

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like