【问题标题】:How do I send a PDF to Watson's Document Conversion service without writing it to disk first?如何在不先将 PDF 写入磁盘的情况下将 PDF 发送到 Watson 的 Document Conversion 服务?
【发布时间】:2016-05-23 16:48:34
【问题描述】:

我正在尝试使用 watson-developer-cloud node.js 库将 this document (http://www.redbooks.ibm.com/redbooks/pdfs/ga195486.pdf) 转换为 Watson 文档转换服务中的应答单元。

在实际程序(不是这个测试程序)中,我正在检索文档并即时转换它,而不是先将其写入磁盘。我以前用其他文档做过这个,但是最新版本的库 (v 1.7.0) 似乎已经改变,它不再像我使用它的方式工作了。但即使在我开始使用最新版本之前,这个特定的文档也无法转换。

我使用的带注释的测试代码如下。我已经尝试了几种方法来让它工作,它们的变体都在下面的 var opts={ 下注释掉了。您必须一次取消其中一个注释才能看到结果。

'use strict';
var bluemix = require('./bluemix');
var extend=require('util')._extend;
var fs=require('fs');
var watson=require('watson-developer-cloud');
var streams = require('memory-streams');

var dcCredentials =  extend({
  url: '<url>',
  version: 'v1',
  username: '<username>',
  password: '<password>'
}, bluemix.getServiceCreds('document_conversion')); // VCAP_SERVICES
var document_conversion = watson.document_conversion(dcCredentials);

var bookpdf=getBook('ga195486.pdf'); 
convert(bookpdf);

function getBook(filename)
   {
   var bl=fs.readFileSync(filename,'utf8');
   return bl;
   }

function convert(content)
   {
   var opts={ //uncomment ONE of these
//      file: new Buffer(content), //See message #1 below
//      file: {value: new Buffer(content), options: {}}, //see message #2 below
//      file: {value: new Buffer(content), options: {contentType: "application/pdf"}}, //This used to work. See message #2 (again) below
//      file: new streams.ReadableStream(content),//see message #3 below
      conversion_target: "ANSWER_UNITS",
      content_type:'application/pdf'
      };
   document_conversion.convert(opts,
      function (err, response) 
         {
         if (err) 
            {
            console.log("Error converting doc: ", err);
            }
         else if (response.answer_units.length==0)
            {
            var msg="No answer units";
            console.log(msg,response);
            }
         else 
            {
            console.log('Works!');
            console.dir(response);
            }
         }
      );
   }

//Message #1: This returns: 
//  No answer units { source_document_id: '',
//  timestamp: '2016-05-23T16:18:23.825Z',
//  media_type_detected: 'application/pdf',
//  metadata: [],
//  answer_units: [],
//  warnings: 
//   [ { phase: 'pdf',
//       warning_id: 'empty_input_to_converter',
//       description: 'The input provided to the converter phase is empty or doesn\'t contain text that can be converted.' },
//     { phase: 'normalized_html',
//       warning_id: 'empty_input_to_converter',
//       description: 'The input HTML document has no body content.' },
//     { phase: 'answer_units',
//       warning_id: 'empty_input_to_converter',
//       description: 'The input provided to the converter phase is empty or doesn\'t contain text that can be converted.' } ] }


//Message #2: These return:
///home/david/git/ccb-contentbridge/node_modules/watson-developer-cloud/node_modules/request/node_modules/combined-stream/node_modules/delayed-stream/lib/delayed_stream.js:33
//  source.on('error', function() {});
//
//TypeError: source.on is not a function
//    at Function.DelayedStream.create (/home/david/git/ccb-contentbridge/node_modules/watson-developer-cloud/node_modules/request/node_modules/combined-stream/node_modules/delayed-stream/lib/delayed_stream.js:33:10)
//    at FormData.CombinedStream.append (/home/david/git/ccb-contentbridge/node_modules/watson-developer-cloud/node_modules/request/node_modules/combined-stream/lib/combined_stream.js:43:37)
//    at FormData.append (/home/david/git/ccb-contentbridge/node_modules/watson-developer-cloud/node_modules/request/node_modules/form-data/lib/form_data.js:68:3)
//    at appendFormValue (/home/david/git/ccb-contentbridge/node_modules/watson-developer-cloud/node_modules/request/request.js:339:21)
//    at Request.init (/home/david/git/ccb-contentbridge/node_modules/watson-developer-cloud/node_modules/request/request.js:352:11)
//    at new Request (/home/david/git/ccb-contentbridge/node_modules/watson-developer-cloud/node_modules/request/request.js:142:8)
//    at request (/home/david/git/ccb-contentbridge/node_modules/watson-developer-cloud/node_modules/request/index.js:55:10)
//    at createRequest (/home/david/git/ccb-contentbridge/node_modules/watson-developer-cloud/lib/requestwrapper.js:134:10)
//    at DocumentConversion.convert (/home/david/git/ccb-contentbridge/node_modules/watson-developer-cloud/services/document_conversion/v1.js:134:10)
//    at convert (/home/david/git/ccb-contentbridge/testRedbooks.js:35:24)


//Message #3: This returns and then it hangs there:
//Error converting doc:  { code: 400, error: 'Error in the web application' }

谁能告诉我我做错了什么?

【问题讨论】:

  • 我在看这个。您是否有在 v1.7.0 之前有效而在 v1.7.0 之后不再有效的示例?

标签: ibm-cloud ibm-watson document-conversion


【解决方案1】:

该特定文件大于文档转换服务当前可以处理的文件。不幸的是,我没有关于目前限制的确切信息,但团队已经意识到这一点并正在寻求改进。

如果您能提供一个以前有效但与 node.js 库的 v1.7.0 冲突的示例,我会看一下,希望能够提供更好的信息。

哦,在您的 fs.readfileSync() 调用中指定 'utf8' 可能会导致您遇到一些问题。

【讨论】:

  • 显然我尝试转换的所有红皮书文档都超过了大小限制。我没有意识到甚至有大小限制,而且我收到的错误也没有表明这一点。如果我只使用低于 1 兆字节左右的 PDF 文件,第一个选项(文件:新缓冲区(内容))似乎可以正常工作。关于'utf8'参数,我不知道当我把它放在那里时我在想什么。它不是“真实”代码的一部分,只是这个测试程序。
【解决方案2】:

Doc Con 的限制似乎是 50 MB per this,而我们的文档比这小...一定是其他问题。

【讨论】:

    猜你喜欢
    • 2018-01-09
    • 2010-11-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-17
    • 1970-01-01
    • 2021-04-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多