捕獲和轉換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創建Word文檔。 您必須通過 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")