
使用 Ruby SDK 将 PDF 转换为 Word 或将 Word 转换为 PDF
许多文字文档处理应用程序都具有处理 MS Word 和 OpenOffice 格式的功能,并且还允许您将输出呈现为其他文件格式,包括 PDF。但是,使用此类软件无法执行批量转换操作,在所有此类情况下,编程解决方案是一种可行的方法。在本文中,我们将讨论使用 Ruby SDK 将 DOC 转换为 PDF 以及将 PDF 转换为 DOC。
- DOC 到 PDF 转换 API
- 使用 Ruby 将 DOC 转换为 PDF
- 使用 Ruby 将 PDF 转换为 DOC
- 使用 cURL 命令将 DOC 转换为 PDF
- 使用 cURL 命令将 PDF 转换为 DOC
DOC 到 PDF 转换 API
Aspose.Words Cloud 是一款令人兴奋的解决方案,可用于创建、操作和转换领先的 Word 文档格式。现在,为了在 Ruby 应用程序中实现 Word 到 PDF 的转换例程,我们将安装 Aspose.Words Cloud SDK for Ruby。该 SDK 可作为 Ruby GEM 和 GitHub 下载。因此,一旦您配置了 Ruby 运行时,请在终端上执行以下命令以执行安装。
gem install aspose_words_cloud
但是在继续安装 Aspose.Words Cloud SDK for Ruby 之前,您需要安装以下依赖项。
运行时依赖项(3)
开发依赖项(1)
安装完成后,我们需要通过访问 Aspose.Cloud 仪表板 创建一个免费帐户。使用您的 GitHub 或 Google 帐户注册,或单击 创建新帐户 按钮。请注意您的客户端凭据,因为我们将在以下部分中使用它们。
使用 Ruby 将 DOC 转换为 PDF
请按照下面给出的说明使用 Ruby 语言执行 Word 到 PDF 的转换。
- 第一步是创建变量,保存来自仪表板的 ClientID 和 ClientSecret 详细信息
- 其次,从 AsposeWordsCloud 创建 configure 实例并传递 ClientID 和 ClientSecret 值
- 第三,创建WordsAPI的实例。
- 现在使用 UploadFileRequest(…) 方法将源 DOCX 文件上传到云存储。
- 下一步是创建一个 ConvertDocumentRequest(..) 对象,该对象以输入的 DOCX 名称和结果格式作为参数。
- 最后调用convertdocument(..)方法执行转换操作。
require 'aspose_words_cloud'
# 来自 https://dashboard.aspose.cloud/ 的客户端凭据
client_id = "718e4235-8866-4ebe-bff4-f5a14a4b6466"
client_secret = "388e864b819d8b067a8b1cb625a2ea8e"
# 将配置属性与 WordsApi 关联
AsposeWordsCloud.configure do |config|
config.client_data['ClientId'] = client_id
config.client_data['ClientSecret'] = client_secret
end
# 创建 WordsApi 实例
@words_api = WordsAPI.new
# 输入 DOCX 文件
fileName = "test_multi_pages.docx"
# 结果格式
format = "pdf"
# 将原始文档上传至云存储
@words_api.upload_file UploadFileRequest.new(File.new(fileName, 'rb'), fileName, nil)
# 定义文档转换参数
request = ConvertDocumentRequest.new(File.new(fileName, 'rb'), format, nil, nil, nil, nil)
# 启动 DOCX 到 PDF 的转换过程
result = @words_api.convert_document(request)
# 在控制台中打印结果响应
puts("Result " + (result).to_s)
使用 Ruby 将 PDF 转换为 DOC
PDF 是广泛使用的文档格式之一,能够封装文本、表格、光栅/矢量图形、视频和音频数据。它还能在任何平台上保持文档保真度,因此,我们收到大量 PDF 格式的文档。但为了编辑它们,您需要特定的应用程序,在本节中,我们将使用 Ruby 开发 PDF 到 Word 的转换应用程序。
- 首先,我们需要创建一个 WordsAPI 对象,在其中提供 ClientID 和 ClientSecret 详细信息。
- 其次,创建一个 SaveAsRequest 实例,该实例以指定目标格式和输出文件名的 SaveOptionsData 对象作为参数。
- 最后,调用saveas(..)方法并传递SaveAsRequest对象来执行转换。
require 'aspose_words_cloud'
# 来自 https://dashboard.aspose.cloud/ 的客户端凭据
client_id = "718e4235-8866-4ebe-bff4-f5a14a4b6466"
client_secret = "388e864b819d8b067a8b1cb625a2ea8e"
# 将配置属性与 WordsApi 关联
AsposeWordsCloud.configure do |config|
config.client_data['ClientId'] = client_id
config.client_data['ClientSecret'] = client_secret
end
# 创建 WordsApi 实例
@words_api = WordsAPI.new
# 输入 DOCX 文件
fileName = "test_multi_pages.docx"
# 结果格式
format = "pdf"
# 将原始文档上传至云存储
@words_api.upload_file UploadFileRequest.new(File.new(fileName, 'rb'), fileName, nil)
# 定义文档转换参数
request = ConvertDocumentRequest.new(File.new(fileName, 'rb'), format, nil, nil, nil, nil)
# 启动 DOCX 到 PDF 的转换过程
result = @words_api.convert_document(request)
# 在控制台中打印结果响应
puts("Result " + (result).to_s)
使用 cURL 命令将 DOC 转换为 PDF
cURL 命令是在任何平台上访问 REST API 的一种令人兴奋的方式。由于 Aspose.Words Cloud 是按照 REST 原则开发的,因此我们可以使用它们来执行转换操作。但是,在继续之前,我们需要首先根据从 Aspose.Cloud 仪表板 检索到的 ClientID 和 ClientSecret 详细信息生成 JSON Web Token (JWT)。请在终端中执行以下命令以生成 JWT 令牌。
curl -v "https://api.aspose.cloud/connect/token" \
-X POST \
-d "grant_type=client_credentials&client_id=718e4235-8866-4ebe-bff4-f5a14a4b6466&client_secret=388e864b819d8b067a8b1cb625a2ea8e" \
-H "Content-Type: application/x-www-form-urlencoded" \
-H "Accept: application/json"
生成新的 JWT 令牌后,使用以下命令将 DOC 文件转换为 PDF 格式。name 参数表示输入的 DOCX 文件已在云存储中可用,format 参数表示结果文件的格式,outPath 显示结果 PDF 文档的位置。转换是使用 GetDocumentWithFormat API 执行的,在下面给出的示例中,testmultipages.docx 文件被呈现为 PDF 格式并以 MyConverted.pdf 的名称保存。
curl -X GET "https://api.aspose.cloud/v4.0/words/test_multi_pages.docx?format=pdf&outPath=MyConverted.pdf" \
-H "accept: application/octet-stream" \
-H "Authorization: Bearer <JWT Token>"
使用 cURL 命令将 PDF 转换为 DOC
可以使用 cURL 命令将 PDF 转换为 DOC/DOCX 和其他受支持的 Word 文档格式。为此,我们需要使用 ConvertDocument API。
curl -X PUT "https://api.aspose.cloud/v4.0/words/converted.pdf/saveAs" \
-H "accept: application/json" \
-H "Authorization: Bearer <JWT Token>" \
-H "Content-Type: application/json" \
-d "{\"SaveFormat\":\"docx\",\"FileName\":\"output.docx\",\"AllowEmbeddingPostScriptFonts\":true,\"ZipOutput\":false,\"UpdateLastSavedTimeProperty\":true,\"UpdateSdtContent\":true,\"UpdateFields\":true,\"Dml3DEffectsRenderingMode\":\"Basic\",\"UpdateCreatedTimeProperty\":true,\"UpdateLastPrintedProperty\":true}"
结论
在上面的文章中,我们讨论了使用 Aspose.Words Cloud SDK for Ruby 将 DOCX 文件转换为 PDF 以及将 PDF 转换为 DOC/DOCX 文件的一种令人兴奋且方便的方法。请注意,我们所有的云 SDK 都是开源的,同样,Aspose.Words cloud SDK for Ruby 的完整代码可在 GitHub 上找到。
相关链接
我们还建议您访问以下链接以获取相关详细信息