使用 Ruby 将多种文件类型组合并合并到一个文档中

使用 Ruby 将多种文件类型合并到一个文档中

合并相同或不同类型的不同文档允许将分散的数据或信息收集到一个文件中。我们可以轻松地将多个不同文件类型的文档合并到云端的一个文件中。在本文中,我们将学习如何使用 Ruby REST API 将多种文件类型合并到一个文档中。

本文应涵盖以下主题:

文件合并 REST API 和 Ruby SDK

为了合并多个文件,我们将使用 GroupDocs.Merger Cloud API 的 Ruby SDK。它使我们能够从 WordExcelPowerPoint支持的文档格式 中合并、拆分、删除和重新排列单个页面或一组页面, Visio 绘图PDFHTML。请在控制台中使用以下命令安装它:

gem install groupdocs_conversion_cloud

在执行上述步骤之前,请从仪表板获取您的客户端 ID 和密码。获得 ID 和密码后,添加如下所示的代码:

# 加载多种文件类型合并 gem
require 'groupdocs_merger_cloud'
@client_id = "xxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxxx"
@client_secret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

# 定义您的存储名称
@mystorage = "MYStorage"

使用 Ruby 将多种文件类型合并到一个文档中

我们可以按照下面给出的步骤以编程方式在云上组合多种文件类型的文档。您可以将文档上传 到云端,因此上传的文件将在云端仪表板的文件部分 中可用。现在,我们可以按照以下步骤将不同类型的文件合并为一个文件:

  • 首先,创建 DocumentApi 的实例。
  • 接下来,为第一个 JoinItem 提供输入文件路径。
  • 然后,为第二个 JoinItem 提供输入文件路径。
  • 或者,重复上述步骤以添加更多文件。
  • 之后,定义 JoinOptions 并设置输出文件的路径。
  • 最后,调用 join() 方法并保存合并后的文档。

以下代码示例展示了如何在 Ruby 中使用 REST API 合并不同的文件类型。

# 如何将 PDF 和 Excel 合并为 PDF?
# 创建文档 API 的实例
@documentApi = GroupDocsMergerCloud::DocumentApi.from_keys(@client_id, @client_secret)

@item1 = GroupDocsMergerCloud::JoinItem.new
@item1.file_info = GroupDocsMergerCloud::FileInfo.new
@item1.file_info.file_path = 'combine-multiple-files/two-pages.pdf'
@item1.file_info.password = 'password'

@item2 = GroupDocsMergerCloud::JoinItem.new
@item2.file_info = GroupDocsMergerCloud::FileInfo.new
@item2.file_info.file_path = 'combine-multiple-files/four-pages.docx'       

@options = GroupDocsMergerCloud::JoinOptions.new
@options.join_items = [@item1, @item2]
@options.output_path = "combine-multiple-files/combined.pdf"

@result = @documentApi.join(GroupDocsMergerCloud::JoinRequest.new(@options))

puts("Resultant file path: " + @result.path)
puts("Successfully combined multiple document types in Ruby.")

最后,上面的代码示例会将合并后的 PDF 文件保存在云端。输出文档应包含 PDF 文档的所有页面,然后是 Word 文档的所有页面。

如何将 PDF 和 Excel 合并为 PDF

我们可以按照前面提到的步骤将PDF和Excel文件合并成一个PDF。但是,我们只需要提供 PDF 和 Excel 文档路径作为第一个和第二个 JoinItems。以下代码示例展示了如何使用 Ruby 中的 REST API 将 PDF 文档和 Excel 工作表合并为 PDF 文件。

# 如何将 PDF 和 Excel 合并为 PDF?
# 创建文档 API 的实例
@mergerApi = GroupDocsMergerCloud::DocumentApi.from_keys(@client_id, @client_secret)

@item1 = GroupDocsMergerCloud::JoinItem.new
@item1.file_info = GroupDocsMergerCloud::FileInfo.new
@item1.file_info.file_path = 'combine-multiple-files/two-pages.pdf'
@item1.file_info.password = 'password'

@item2 = GroupDocsMergerCloud::JoinItem.new
@item2.file_info = GroupDocsMergerCloud::FileInfo.new
@item2.file_info.file_path = 'combine-multiple-files/four-pages.xlsx'       

@options = GroupDocsMergerCloud::JoinOptions.new
@options.join_items = [@item1, @item2]
@options.output_path = "combine-multiple-files/combined.pdf"

@result = @documentApi.join(GroupDocsMergerCloud::JoinRequest.new(@options))

puts("Resultant file path: " + @result.path)
puts("Successfully combined PDF and Excel into PDF using Ruby.")

如何将 PDF 和 PowerPoint 合并为 PDF

我们还可以按照前面提到的步骤,通过在 Ruby 中融合 REST API,将 PDF 文档和 PowerPoint 演示文稿合并为 PDF。但是,我们只需要提供 PDF 和 PowerPoint 文档路径作为第一个和第二个 JoinItems。以下代码示例展示了如何使用 Ruby 中的 REST API 将 PDF 文档和 PowerPoint 演示文稿合并为 PDF 文件。

# 如何将 PDF 和 PowerPoint 合并为 PDF?
# 创建文档 API 的实例
@documentApi = GroupDocsMergerCloud::DocumentApi.from_keys(@client_id, @client_secret)

@item1 = GroupDocsMergerCloud::JoinItem.new
@item1.file_info = GroupDocsMergerCloud::FileInfo.new
@item1.file_info.file_path = 'combine-multiple-files/two-pages.pdf'
@item1.file_info.password = 'password'

@item2 = GroupDocsMergerCloud::JoinItem.new
@item2.file_info = GroupDocsMergerCloud::FileInfo.new
@item2.file_info.file_path = 'combine-multiple-files/five-pages.pptx'       

@options = GroupDocsMergerCloud::JoinOptions.new
@options.join_items = [@item1, @item2]
@options.output_path = "combine-multiple-files/combined.pdf"

@result = @documentApi.join(GroupDocsMergerCloud::JoinRequest.new(@options))

puts("Resultant file path: " + @result.path)
puts("Successfully combined PDF and PowerPoint into PDF using Ruby.")

在 Ruby 中组合不同文件类型的特定页面

我们可以按照以下步骤将不同类型文档中的选定页面合并到一个文件中:

  • 首先,创建 DocumentApi 的实例。
  • 接下来,为第一个 JoinItem 提供输入文件路径。
  • 然后,提供要合并的特定页码。
  • 接下来,为第二个 JoinItem 提供输入文件路径。
  • 然后,定义要与起始页码和结束页码合并的页面范围。
  • 之后,定义 JoinOptions 并设置输出文件的路径。
  • 最后,调用 join() 方法并保存合并后的文档。

以下代码示例展示了如何使用 Ruby 中的 REST API 合并不同文件类型的特定页面。

# 如何在 Ruby 中合并多种文件类型的特定页面?
# 创建文档 API 的实例
@documentApi = GroupDocsMergerCloud::DocumentApi.from_keys(@client_id, @client_secret)

@item1 = GroupDocsMergerCloud::JoinItem.new
@item1.file_info = GroupDocsMergerCloud::FileInfo.new
@item1.file_info.file_path = 'combine-multiple-files/ten-pages.pdf'
@item1.pages = [3, 6, 8]

@item2 = GroupDocsMergerCloud::JoinItem.new
@item2.file_info = GroupDocsMergerCloud::FileInfo.new
@item2.file_info.file_path = 'combine-multiple-files/four-pages.docx'       
@item2.start_page_number = 1
@item2.end_page_number = 4

@options = GroupDocsMergerCloud::JoinOptions.new
@options.join_items = [@item1, @item2]
@options.output_path = "combine-multiple-files/combined.pdf"

@result = @documentApi.join(GroupDocsMergerCloud::JoinRequest.new(@options))

puts("Output file path: " + @result.path)
puts("Merged multiple types documents pages in Ruby.")

在线多种文件类型合并

请试用以下使用上述API开发的免费在线合并工具。 https://products.groupdocs.app/merger/

结论

在本文中,我们了解到:

  • 如何在 Ruby 中合并多种文件类型的文档;
  • 如何将PDF和Excel合并成PDF;
  • 如何将 PDF 和 PowerPoint 合并为 PDF;

此外,您可以使用 文档 了解有关 GroupDocs.Merge Cloud API 的更多信息。我们还提供了一个 API 参考 部分,让您可以直接通过浏览器可视化我们的 API 并与之交互。

问一个问题

如果您对我们的多文件类型合并有任何疑问,请随时在论坛 上询问我们

也可以看看