6

I'm trying to convert PDF to any allowed format but response does not content exportLinks after a pdf file was uploaded.

I've already tried many ways to manage for to get exportLinks. Other doc formats can be uploaded and be exported successfully. I have used code from developers.google.com.

media_body = MediaFileUpload(filename, mimetype=mime_type, resumable=False)
body = {'title': title,
        'description': description,
        'mimeType': mime_type,
        }
# Set the parent folder.
if parent_id:
    body['parents'] = [{'id': parent_id}]

try:
    request = service.files().insert(body=body, media_body=media_body)
    #request.uri = "%s&convert=true&ocr=true" % request.uri
    #request.uri = "%s&convert=true" % request.uri
    request.uri = "https://www.googleapis.com/upload/drive/v2/files?uploadType=multipart&alt=json&convert=true"
    logger.debug("Request: %s", request.uri)
    file_obj = request.execute()
    # Uncomment the following line to print the File ID
    # print 'File ID: %s' % file_obj['id']
    import json
    logger.debug('Get file result: %s' % json.dumps(file_obj, indent=4))
    return file_obj

...
export_links = source_file.get('exportLinks')
if not export_links:
    logger.debug('Warning: exportLinks does not exist')

Why there is no exportLinks in the PDF upload's response?

Please, Google Team agree that PDF can be uploaded and exported by Drive API.

Thanks

[UPD] Here is a request dump of Httplib2. As I see query is set properly according to manual.

Request: https://www.googleapis.com/upload/drive/v2/files?uploadType=multipart&alt=json&convert=true
send: 'POST /upload/drive/v2/files?uploadType=multipart&alt=json&convert=true HTTP/1.1\r\n
Host: www.googleapis.com\r\n
content-length: 4425\r\n
accept-encoding: gzip, deflate\r\n
accept: application/json\r\n
user-agent: google-api-python-client/1.0\r\n
content-type: multipart/related; boundary="===============0758269123680398078=="\r\n
authorization: Bearer ya29.AHES6ZRDT0AJX8a5JN_u_0epF6ZCMGcqwyru1wzj22oVlMO9CfmFzlo\r\n
\r\n
--===============0758269123680398078==\nContent-Type: application/json\nMIME-Version: 1.0\n\n{"mimeType": "application/pdf", "description": "", "title": "Short_pdf.pdf"}\n--===============

Here is a downloadLink in the response: "downloadUrl": "https://doc-0c-2c-docs.googleusercontent.com/docs/securesc/2sgoib42bclb5coaj85g5574fd2b65bn/2lnqk989jdaepp4b8igdco1j8fu7m9l3/1373040000000/00639208221381605991/00639208221381605991/0By108D1dgCDkX0NFOXV0V2pNd2s?h=16653014193614665626&e=download&gd=true",

  • Does the response contain a download link? If so, conversion has not taken place. Also, please set query params by passing params to insert method: `.insert(body=b, media_body=m, convert=True)` – Burcu Dogan Jul 05 '13 at 12:05
  • I've updated the post. Please inspect what is wrong with request. Also, I tried as you suggested to pass convert param - no success. Request header is the same as in the update. I suppose you meant that download link is "downloadUrl". Thanks – Andrey Aleksandrov Jul 05 '13 at 18:47

1 Answers1

4

Unfortunately Google Drive does not provide conversion capabilities for PDF files through the Google Drive API.

Only Google native documents (Google Docs/Sheets/Slides/Drawings) have exportLinks allowing to download the Google document in multiple export formats. All other files - regular files basically such as PDF, images etc... - have a downloadUrl instead which allows you to download the file itself in its original format.

Nicolas Garnier
  • 12,134
  • 2
  • 42
  • 39
  • Ok, Thanks. Guys, you should update your manual rather than people have evaporated brain. By the way I can't found any downloadLink yet. There is downloadUrl (https://developers.google.com/drive/manage-downloads). – Andrey Aleksandrov Jul 09 '13 at 20:33
  • Sorry it is actually downloadUrl, downloadLink was a typo :) – Nicolas Garnier Jul 10 '13 at 13:56
  • Could you tell us which part of the documentation you would update. The doc mentions "Download and export a Google Doc — exportLinks". In this context Google Doc means: Google Document/Spreadsheet/Presentations/Drawings. PDFs are regular files and not Google Docs therefore qualifies for 'downloadUrl'. – Nicolas Garnier Jul 10 '13 at 13:59
  • Yes I could, last link is one. There no any exact and explicit part what specifies the problem. "The file resource includes exportLinks URLs for the various export formats for Google Documents (Documents, Spreadsheets, Presentations, *etc*.).". What is *etc*? Perhaps is it Drawing? The other part is table that described as "Google Doc formats and supported export MIME types map to each other as follows:". According to this table - PDF is Google Doc Format and is Drawing. It is not regular as you noted. downloadUrl is retrieving file itself, not for none Google Doc. – Andrey Aleksandrov Jul 13 '13 at 20:38
  • 1
    @Nivco Have google added this capability of pdf to google doc conversion and ocr conversion through API in v3. I am not able to find how to do that, but since there is an ocrLanguage parameter in files.create(), I suppose they have it. – mridul Jan 02 '17 at 08:51