How To Insert HTML in Microsoft Word Document With RUBY Using Aspose.Words Cloud

Hi,

I am trying to understand the insert HTML and insert image thing with the ruby aspose cloud API
I have a simple template Standard method fields.docx (12.6 KB)
with 3 fields -

  1. field1 - for simple text (let say Apple)
  2. field2 - for Html data (let say <i>this is italic</i>)
  3. field3 - for image (use This image url for example )

I have looked at the documentation but I am not able to construct the JSON data to populate the template appropriately.
I also looked at this topic but the files are private.
Can anybody tell me the json data format needed to be sent in a request using the data given above?

@nikhilgoyal2205

Please find the sample template and data to insert HTML in Microsoft Word Document with Ruby using Aspose.Words Cloud SDK for Ruby. Hopefully, it will help you to accomplish the task.

HtmlImageTemplate.zip (11.8 KB)

Steps to Insert HTML Code in Microsoft Word Document With Ruby

  • Signup for Free Trial and get the credentials
  • Install Aspose.Words Cloud SDK for Ruby gem from RubyGems.org
  • Create a script file for code and import aspose_words_cloud package
  • initiate Aspose.Words Cloud API
  • upload template file to cloud storage
  • Create ExecuteMailMergeRequest object
  • Call execute_mail_merge method to insert HTML in Word Document

Sample Code To Embed HTML in Microsoft Word Document With Ruby

require 'aspose_words_cloud'

class Document

  include AsposeWordsCloud

  # Get Client ID and Secret from https://dashboard.aspose.cloud/
  Client_Secret = "xxxxxxxxxxxxxxxxxxxxxxxxxxx"
  Client_ID = "xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx"
  FILE_PATH = "C:/Temp/MailMergeSamples/"

  def initialize
    AsposeWordsCloud.configure do |config|
      config.client_data['ClientId'] = Client_ID
      config.client_data['ClientSecret'] = Client_Secret
	  end
    @words_api = WordsApi.new
  end

  # MailMerge
  def execute_mailmerge
      local_document_file = 'TestExecuteTemplate.docx'
      remote_file_name = 'HtmlImageTemplate.docx'
      remote_file_output = 'HtmlImageTemplate_out.docx'
      local_data_file = File.read(FILE_PATH + 'TestDataWithRemoteImagesHTML.xml')
      
      # upload Word template to cloud storage
      request_file_content = File.open(FILE_PATH+ local_document_file)
      request_upload = UploadFileRequest.new(file_content: request_file_content, path: remote_file_name)
      @words_api.upload_file(request_upload)

      #Insert HTML to Word document
      request = ExecuteMailMergeRequest.new(name: remote_file_name, data: local_data_file, with_regions: true, dest_file_name: remote_file_output)

      result = @words_api.execute_mail_merge(request)
end
end
	
	
document = Document.new()
puts document.execute_mailmerge

@tilal.ahmad
Thanks for the prompt response. So, this is with XML format. Generally, we don’t deal with XML in our code. Can you give the same in JSON format? I tried converting the XML data into JSON using different converters but the API is giving an internal server error.
Any help would be greatly appreciated. Thanks!

@nikhilgoyal2205

Please find the JSON data for your reference. Hopefully, it will help you to accomplish the task.

{
   "Item": {
       "field1": "This is the first caption !!!",
      "field2": {
         "format": "html",
         "htmlText": "<html><head><style \ntype=\"text/css\">h2{color:green} .color_red{color:red} .color_green{color:green} .color_magenta{color:magenta} \n.color_olive{color:olive} .color_teal{color:teal}<\/style><\/head><body><h2>Table<\/h2><table \nborder=\"2\" cellspacing=\"0\" cellpadding=\"4\"><tr><th class=\"color_red\">Column \n1<\/th><th class=\"color_green\">Column 2<\/th><\/tr><tr><td \nclass=\"color_magenta\">Value 1.1<\/td><td class=\"color_olive\">Value \n1.2<\/td><\/tr><tr><td class=\"color_teal\">Value \n2.1<\/td><\/tr><\/table><h2>Div<\/h2><div class=\"color_olive\">Outer text<div \nclass=\"color_magenta\">Inner text<\/div>Outer text<\/div><h2>Image<\/h2><br/><img \nsrc=\"data:image/gif;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAACXBIWXMAAAsTAAALEwEAmpwYAAADUUlEQVR42lWTWUwTURSGaUtIKY\n5QweISTUzceDGu0Rj1xURjTNQEE+ODGn0hPhBEQdqZztahBbrAtFih1Fprh9IBiVLLErZSU7BUoS7VqNH44ovx0S0ovdfO4Ez04cw9Ofec7/5zzr15NEnkUxSVFx8ObZ1x7nzH\nN6z9nOhnzOFwWCnECcKoEFbBRrrp6iF/XRvD0BoplkdTuAiIhtt3P3Fu+jJlKlkM1WoXLFjVeSnpXrC94gV3ciJp1X0L1K7+NtDfvXdpj1TmmUwm8QTfLa867T+ceWTSwO5r5b\n87XNaDEoBrv3p2Ai+AU1Qh5OiDGS4UXi4roAisQHBiYfJS2obAVPMyELYcGxViOIaKcLaNLXzadej5nBUBPXW6hdYW0z4RQOYUXK2tUfr8QeXL0Kn4JKEBk7QWdtrrLggJJEmo\nCBxVCf50qIaaa0HABFkEPZaLNnGfwPNFGV0u05Y554bvMQoBPLbxs9t9QydJpHP/KayRbsf+pG1NdgTTgDuGXa8dbS41ZtArxaQYT1WlHVqYaloGOcvJIan4X2MdViTl3vExbS\n+GPfXlvzzutj1LPch9HrYea59t0YIRtAAO+PQuIWYwNKikYty41Iuk/3TvMzsCB9HcQR7msgzgjZs7Z5rLQPR6PojeJW2d3oDCoG/IlwAYqlexbp/i9eB19ziuBoMoAlrR03YZ\n0G8+0JFhS8EjuhD6zWfuCTEjppcBOKYX1bwaqA5MM2oYZ4pBy7XjrAyY7sPqU01FcNZW/jtwZeXXVpt5u1RM4JhYzHFBddqz5/28fUU2NwngtVUbZUCE79o6x67/MYiXLiSbS8\nD9xp1vI7znkLvDK07A5/MiT7mzdxMMAsdMqxaiWDHkgzePigBUXy9KTfdWeVKNSjjPrvv5uKkYxszlIHX7eDLeVfkgbt30bt5eAlOOtT9nLRrI03szrOumOtcb4SrTSj2KKfr4\nnuUZ7kR8HFPAYXLlYqxRl43TGjjTWAhHCC1MNK/+lWA0cJLRgfsh95G/N1H136z7+BCSuHMuMEGVLo4a1XCKKcuO0WXZBw1FcNighmPWbe8jIeeJpdFiCrkHguWUyM92NBKseB\nut8T92bfsw66z49IyrHH2T5CvlqeCYUvL/ANIZ7+ohXSdWAAAAAElFTkSuQmCC\"><\/body><\/html>"
      },
      "field3": "https://www.google.ru/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png"
   }
}

@tilal.ahmad
Thanks for the reply and help. Works for my current scenarios. If anything complicated pops up, I will reach out.

Thanks again!

1 Like