Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML2PDF using Google Drive API

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

like image 507
proppy Avatar asked Oct 11 '12 16:10

proppy


People also ask

Is Google Drive API free?

Pricing. All use of the Drive API is available at no additional cost.

How retrieve data from Google Drive in PHP?

This Google API class helps to authenticate with a Google account and access the Drive API with REST API using PHP cURL. This custom PHP library will use Google Drive API v3 to handle the file download process. GetAccessToken() – Fetch the access token from Google OAuth 2 API using the authentication code.


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.

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

like image 156
Ali Afshar Avatar answered Sep 29 '22 01:09

Ali Afshar


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();
}
like image 44
David Avatar answered Sep 29 '22 02:09

David