捕获和转换Web的工具

将URL和HTML转换为DOCXRuby API

添加转换HTML或网页的功能 into将Word文档发送到您的应用程序从未如此简单 GrabzIt的Ruby API。 但是,在开始之前,请记住 url_to_docx, html_to_docx or file_to_docx 方法 save or save_to 必须调用方法才能实际创建DOCX。

基本选项

当DOCX转换整个网页时捕获网页 int可以包含许多页面的Word文档。 只需一个参数即可转换网页 int文字文件或 将HTML转换为DOCX 如以下示例所示。

grabzItClient.url_to_docx("https://www.tesla.com")
# Then call the save or save_to method
grabzItClient.html_to_docx("<html><body><h1>Hello World!</h1></body></html>")
# Then call the save or save_to method
grabzItClient.file_to_docx("example.html")
# Then call the save or save_to method

自订识别码

您可以将自定义标识符传递给 docx文档 方法,如下所示,然后将该值返回给您的GrabzIt Ruby处理程序。 例如,此自定义标识符可以是数据库标识符,从而允许DOCX文档与特定数据库记录相关联。

grabzItClient = GrabzIt::Client.new("Sign in to view your Application Key", "Sign in to view your Application Secret")

options = GrabzIt::DOCXOptions.new()
options.customId = "123456"

grabzItClient.url_to_docx("https://www.tesla.com", options)
# Then call the save method
grabzItClient.save("http://www.example.com/handler/index")
grabzItClient = GrabzIt::Client.new("Sign in to view your Application Key", "Sign in to view your Application Secret")

options = GrabzIt::DOCXOptions.new()
options.customId = "123456"

grabzItClient.html_to_docx("<html><body><h1>Hello World!</h1></body></html>", options)
# Then call the save method
grabzItClient.save("http://www.example.com/handler/index")
grabzItClient = GrabzIt::Client.new("Sign in to view your Application Key", "Sign in to view your Application Secret")

options = GrabzIt::DOCXOptions.new()
options.customId = "123456"

grabzItClient.file_to_docx("example.html", options)
# Then call the save method
grabzItClient.save("http://www.example.com/handler/index")

页眉和页脚

要将页眉或页脚添加到Word文档中,可以请求您要应用特定的 模板 生成的DOCX。 该模板必须是 saved并会指定页眉和页脚的内容以及任何特殊变量。 在下面的示例代码中,用户正在使用他们创建的名为“我的模板”的模板。

grabzItClient = GrabzIt::Client.new("Sign in to view your Application Key", "Sign in to view your Application Secret")

options = GrabzIt::DOCXOptions.new()
options.templateId = "my template"

grabzItClient.url_to_docx("https://www.tesla.com", options)
# Then call the save or save_to method
grabzItClient.save_to("result.docx")
grabzItClient = GrabzIt::Client.new("Sign in to view your Application Key", "Sign in to view your Application Secret")

options = GrabzIt::DOCXOptions.new()
options.templateId = "my template"

grabzItClient.html_to_docx("<html><body><h1>Hello World!</h1></body></html>", options)
# Then call the save or save_to method
grabzItClient.save_to("result.docx")
grabzItClient = GrabzIt::Client.new("Sign in to view your Application Key", "Sign in to view your Application Secret")

options = GrabzIt::DOCXOptions.new()
options.templateId = "my template"

grabzItClient.file_to_docx("example.html", options)
# Then call the save or save_to method
grabzItClient.save_to("result.docx")

将HTML元素转换为DOCX

如果只想直接转换div或span等HTML元素 int您可以使用GrabzIt的Ruby Gem创建Wo​​rd文档。 您必须通过 CSS选择器 您希望转换为HTML元素的 targetElement 的方法 DOCXOptions 类。

...
<span id="Article">
<p>This is the content I am interested in.</p>
<img src="myimage.jpg">
</span>
...

在此示例中,我们希望捕获跨度中ID为的所有内容 Article,因此我们将其传递给GrabzIt API,如下所示。

grabzItClient = GrabzIt::Client.new("Sign in to view your Application Key", "Sign in to view your Application Secret")

options = GrabzIt::DOCXOptions.new()
options.targetElement = "#Article"

grabzItClient.url_to_docx("http://www.bbc.co.uk/news", options)
# Then call the save or save_to method
grabzItClient.save_to("result.docx")