DOCX to PDF Conversion from Azure Blob using Aspose.Words REST API Issue

I am reading file docx from azure blob and then with below code converting it to pdf:, pdf is created but not opening.
error while opening pdf file:
We can’t open this file
Something went wrong.

var convertRequest = new ConvertDocumentRequest(_documentOperationService.GetFileByUrl(outputfilepath), “pdf”);
var pdffile= wordsApi.ConvertDocument(convertRequest).Result;
IFormFile pdfile = new FormFile(pdffile, 0, a.Document.First().Value.Length, “name”, newFileName.Replace(“docx”,“pdf”));
_documentOperationService.Upload(libraryName, pdfile);

@vmdmehdi

I am afraid it is difficult to comment on the issue without reproducing it at our end. You may please double check that the input document is a valid DOCX and save the output PDF to your local drive. Hopefully, it will help you identify the issue.

var requestDocument = File.OpenRead(localFile);
var convertRequest = new ConvertDocumentRequest(requestDocument, "pdf");
var actual = await wordsApi.ConvertDocument(convertRequest);            
var fileStream = System.IO.File.Create(@"C:\Temp\Output.pdf");
actual.CopyTo(fileStream);
fileStream.Close();

hi,

Fist I used merge method of aspose to merge the document and then upload it to blob. tilll here its working fine, merged word file is also created .

No with url of blob I am reading the word file and getting stream with below method;
public Stream GetFileByUrl(string url)
{
BlobContainerClient blobContainerClient = new BlobContainerClient(_storageconnectionstring, _blobName);
url = Bloburl;
BlobClient blobClientsourcePath = blobContainerClient.GetBlobClient(url);
var stream = blobClientsourcePath.OpenRead();
return stream;
}

now this stream i am passing to create pdf::
var convertRequest = new ConvertDocumentRequest(stream, “pdf”);
var pdffile= wordsApi.ConvertDocument(convertRequest).Result;
IFormFile pdfile = new FormFile(pdffile, 0, a.Document.First().Value.Length, “name”,

then upoading again to blob:
_documentOperationService.Upload(libraryName, pdfile);

pdf is created and uploaded but pdf is corupted.

please help.

If i try converting same merged file from any other online word to pdf converter it working fine. so the merged word file is good. please check my code for converting to pdf or reading word file into stream before converting has any issue.

@vmdmehdi

I am afraid I cannot test your code. However, can you please share your workflow and which API methods you are using for mail merge? I will guide you accordingly.

step 1: read file from azure blob:

public Stream GetFileByUrl(string url)
{
BlobContainerClient blobContainerClient = new BlobContainerClient(_storageconnectionstring, _blobName);
url = url.Replace(“https://***.blob.core.windows.net/root/”, String.Empty);
BlobClient blobClientsourcePath = blobContainerClient.GetBlobClient(url);
var stream = blobClientsourcePath.OpenRead();
return stream;
}

code to convert to pdf:
WordsApi wordsApi = new WordsApi("**-b3ac", "");

var ob = _documentOperationService.GetFileByUrl(filepath);–as above method code
var convertRequest = new ConvertDocumentRequest(ob, “pdf”);–aspose api for convert
var pdffile= wordsApi.ConvertDocument(convertRequest).Result;–aspose api method

        IFormFile pdfile = new FormFile(pdffile, 0, ob.Length, "name", newFileName.Replace("docx","pdf")); --convert doc to formfile and upload to azure

        _documentOperationService.Upload(libraryName, pdfile);

This is my code. Please help

@vmdmehdi

As suggested above, please save your resultant PDF to your local drive before uploading to Azure to double check that the output is not corrupt.

var actual = await wordsApi.ConvertDocument(convertRequest);
var fileStream = System.IO.File.Create(@"C:\Temp\Output.pdf");
actual.CopyTo(fileStream);
fileStream.Close();

Secondly, You can configure your Azure storage with aspose.cloud and use the SaveAs method to convert files from cloud storage. Please check the Operations>Convert>SaveAs method from Aspose.Words Cloud API reference for details.