Is it possible to upload and convert an HTML file to PDF using Google Drive API without user interaction?
HTML2PDF using Google Drive API
Asked Answered
What did you try? Did you search? Did you read the API documentation? –
Lowther
@Lowther Proppy works at Google, so I'm going to take a wild guess and say he probably tried the obvious stuff before posting. –
Uzia
@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. –
Lowther
@Lowther that was not obvious from the documentation developers.google.com/drive/… 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. –
Embolus
@Lowther 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". –
Uzia
@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. –
Lowther
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
And would it also be possible to convert lets say a docx to pdf and then download it? –
Climactic
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();
}
© 2022 - 2024 — McMap. All rights reserved.