Word 轉 PDF

使用 Ruby SDK 將 PDF 轉換為 Word 或將 Word 轉換為 PDF

許多文字文件處理應用程式提供處理 MS Word 和 OpenOffice 格式的功能,還允許您將輸出呈現為其他文件格式,包括 PDF。但是批量轉換操作無法透過此類軟體執行,在所有這些情況下,程式設計解決方案是一種可行的方法。在本文中,我們將討論使用 Ruby SDK 將 DOC 轉換為 PDF 以及將 PDF 轉換為 DOC。

DOC 到 PDF 轉換 API

Aspose.Words Cloud 是一款令人興奮的解決方案,用於建立、操作和轉換領先的 Word 文件格式。現在,為了在 Ruby 應用程式中實作 Word 到 PDF 的轉換例程,我們將安裝 Aspose.Words Cloud SDK for Ruby。因此,一旦配置了 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 RubyDOCX 文件轉換為 PDF 以及將 PDF 轉換為 DOC/DOCX 文件的令人興奮且方便的方法。請注意,我們所有的雲端 SDK 都是開源的,同樣,Aspose.Words cloud SDK for Ruby 的完整程式碼可在 GitHub 上找到。

相關連結

我們還建議您訪問以下連結以獲取相關詳細信息