Convert Word to HTML without using storage

Have an asp.net web application that needs to convert HTML to docx.
I’m using the .net word-cloud nuget package and can load the HTML using LoadWebDocumentData.
I’d like to convert to docx and stream the file back to the client. I’m only seeing options for saving the docx file to storage and then sending that file to the client’s browser. This means I also have to delete the storage after the conversion is done.

Is there a way to pass a URL, and directly get back a docx stream without any intermediary storage?

Thanks!

@rdorris

To convert HTML to DOCX without using intermediary storage in your ASP.NET web application, you can utilize the Aspose.Words Cloud API. Specifically, you can use the PUT /words/convert endpoint, which allows you to send the HTML content directly in the request body and receive the converted DOCX file as a response stream.

Here’s a basic example of how you can implement this in your application:

  1. Load the HTML content: You can load your HTML content as a string.

  2. Make a request to the Aspose.Words Cloud API: Use the PUT /words/convert endpoint to convert the HTML to DOCX.

  3. Stream the response back to the client: You can then return the DOCX file directly to the client without saving it to storage.

Here’s a sample code snippet demonstrating this process:

using System;
using System.IO;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;

public class DocumentConverter
{
    private static readonly HttpClient client = new HttpClient();

    public async Task ConvertHtmlToDocx(string htmlContent)
    {
        // Set your Aspose.Words Cloud API credentials
        string apiBaseUrl = "https://api.aspose.cloud/v4.0/words/convert";
        string apiKey = "YOUR_API_KEY"; // Replace with your API key

        // Prepare the request
        client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", apiKey);
        var content = new StringContent(htmlContent);
        content.Headers.ContentType = new MediaTypeHeaderValue("text/html");

        // Make the request to convert HTML to DOCX
        var response = await client.PutAsync(apiBaseUrl, content);

        if (response.IsSuccessStatusCode)
        {
            // Stream the DOCX file back to the client
            var docxStream = await response.Content.ReadAsStreamAsync();
            // Here you would return the stream to the client, e.g., using ASP.NET MVC
            // return File(docxStream, "application/vnd.openxmlformats-officedocument.wordprocessingml.document", "converted.docx");
        }
        else
        {
            // Handle error response
            throw new Exception("Error converting document: " + response.ReasonPhrase);
        }
    }
}

In this example, replace "YOUR_API_KEY" with your actual Aspose.Words Cloud API key. The ConvertHtmlToDocx method takes the HTML content as input, sends it to the Aspose API, and streams the resulting DOCX file back to the client.

This approach eliminates the need for temporary storage, allowing for a more efficient conversion process. For more details, you can refer to the Aspose documentation on document conversion here for additional options and settings you might want to consider.

Sources:
[1]: Convert a document to another format|Aspose Words Cloud Docs

Thank you for the reply!
I take it I’ll need to use the HttpClient instead of the nuget package?

I’m sorry, but the previous response is auto-generated and not clear enough. You can do this using SDK, but it’s impossible with one request.
You can look at aspose-words-cloud-dotnet/Aspose.Words.Cloud.Sdk.Tests/BatchTests.cs at master · aspose-words-cloud/aspose-words-cloud-dotnet · GitHub
The batch feature of the API allows the combination of multiple requests. You can specify if one request depends on other.
So, in your case, it would be one request with load document, then get document with format from storage, and third request is delete from storage

I made it work with the HttpClient, thank you.
However, one request would be great: allow me to pass a URL to the HTML file instead of pulling it to my server and then resending it to aspose.

By the way, the code above does not work as “format=” is required when using /words/convert

This is a particular case. We didn’t get any request for such functionality, so I will create a ticket for that matter.

@rdorris
We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): WORDSCLOUD-2958

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.