Our requirement is to convert file stored in database, array of byte to PDF using ConvertAPI. We have tried multiple options but getting different errors as mention below. Can someone please help us with function to convert Byte file format to PDF.
using (var client = new WebClient())
{
client.Headers.Add("accept", "application/octet-stream");
client.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
var resultFile = client.UploadData("https://v2.convertapi.com/doc/to/pdf?Secret=**********", byteTemplate);
}
var requestContent = new MultipartFormDataContent();
ByteArrayContent data = new ByteArrayContent(byteTemplate, 0, byteTemplate.Count());
requestContent.Add(data, "File", "Files");
requestContent.Add(new StringContent("**********"), "Secret");
var authParam = parameters.ContainsKey("secret") ? $"Secret={parameters["secret"]}" : $"Token={parameters["token"]}";
return new HttpClient().PostAsync($"https://v2.convertapi.com/{srcFormat}/to/{dstFormat}?{authParam}", requestContent).Result;
or Cannot access a disposed object. Object name: 'System.Net.Http.MultipartFormDataContent'
public static HttpResponseMessage Convert(string srcFormat, string dstFormat, Dictionary<string, string> parameters, byte[] bytetemp, MemoryStream streamTemplate)
{
var requestContent = new MultipartFormDataContent();
streamTemplate.Position = 0;
requestContent.Add(new StreamContent(streamTemplate), "File", "ABC");
foreach (var parameter in parameters)
{
if (File.Exists(parameter.Value))
{
}
else
{
requestContent.Add(new StringContent(parameter.Value), parameter.Key);
}
}
var authParam = parameters.ContainsKey("secret") ? $"Secret={parameters["secret"]}" : $"Token={parameters["token"]}";
HttpContent rescont = new HttpClient().PostAsync($"https://v2.convertapi.com/{srcFormat}/to/{dstFormat}?{authParam}", requestContent).Result.Content;
String a = rescont.ReadAsStringAsync().Result;
}
Old Code with New URL, Working with old URL
public static byte[] CovertWordtoPdf(byte[] response)
{
byte[] bufferDocxReport;
bufferDocxReport = response;
string reportName = "reportname.doc";
#region Convert DOCX report to PDF format
WebRequest convertToPdfRequest = WebRequest.Create("https://v2.convertapi.com/docx/to/pdf?Secret=************");
convertToPdfRequest.Method = "POST";
var boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x", NumberFormatInfo.InvariantInfo);
convertToPdfRequest.ContentType = "multipart/form-data; boundary=" + boundary;
boundary = "--" + boundary;
using (var requestStream = convertToPdfRequest.GetRequestStream())
{
// Write the file
var buffer = Encoding.ASCII.GetBytes(boundary + Environment.NewLine);
requestStream.Write(buffer, 0, buffer.Length);
buffer = Encoding.UTF8.GetBytes(string.Format("Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"{2}", "name", reportName, Environment.NewLine));
requestStream.Write(buffer, 0, buffer.Length);
buffer = Encoding.ASCII.GetBytes(string.Format("Content-Type: {0}{1}{1}", "application/octet-stream", Environment.NewLine));
requestStream.Write(buffer, 0, buffer.Length);
requestStream.Write(bufferDocxReport, 0, bufferDocxReport.Length);
buffer = Encoding.ASCII.GetBytes(Environment.NewLine);
requestStream.Write(buffer, 0, buffer.Length);
var boundaryBuffer = Encoding.ASCII.GetBytes(boundary + "--");
requestStream.Write(boundaryBuffer, 0, boundaryBuffer.Length);
}
using (var convertToPdfResponse = convertToPdfRequest.GetResponse())
using (Stream convertToPdfResponseStream = convertToPdfResponse.GetResponseStream())
{
bufferDocxReport = ReadToEnd(convertToPdfResponseStream);
}
return bufferDocxReport;
#endregion
}
var requestContent = new MultipartFormDataContent();
streamTemplate.Position = 0;
StreamContent data = new StreamContent(streamTemplate);
requestContent.Add(data, "File", "Files");
requestContent.Add(new StringContent("************"), "Secret");
var authParam = parameters.ContainsKey("secret") ? $"Secret={parameters["secret"]}" : $"Token={parameters["token"]}";
return new HttpClient().PostAsync($"https://v2.convertapi.com/{srcFormat}/to/{dstFormat}?{authParam}", requestContent).Result;
The working solution would be
const string fileToConvert = @"C:\Projects\_temp\test1.docx";
var bytesToConvert = File.ReadAllBytes(fileToConvert);
var url = new Uri("https://v2.convertapi.com/docx/to/pdf?secret=<YourSecret>");
var content = new ByteArrayContent(bytesToConvert);
content.Headers.Add("content-type", "application/octet-stream");
content.Headers.Add("content-disposition", "attachment; filename=\"test1.docx\"");
var fileBytes = new HttpClient().PostAsync(url, content).Result.Content.ReadAsByteArrayAsync().Result;
I would like to point several important things: