olirx
olirx

Reputation: 23

Convert API, new API version docx to PDF

I'm using Convert API to convert docx to PDF. With the old API version everything works good, but I'm trying to migrate to the new API version and when I open the PDF is not a valid document and it will not open. Not sure what I am doing wrong, maybe something about the encoding?

The response that I get from Convert API is a JSON with the File Name, File Size and File Data. Maybe this File Data needs to be processed to create a valid PDF file? if I just write that data in a file it does not work.

public string ConvertReportToPDF(string fileName)
{
    string resultFileName = "";
    key = "xxxxx";

    var requestContent = new MultipartFormDataContent();
    var fileStream = System.IO.File.OpenRead(fileName);
    var stream = new StreamContent(fileStream);
    requestContent.Add(stream, "File", fileStream.Name);

    var response = new HttpClient().PostAsync("https://v2.convertapi.com/docx/to/pdf?Secret=" + key, requestContent).Result;
    FileReportResponse responseDeserialized = JsonConvert.DeserializeObject<FileReportResponse>(response.Content.ReadAsStringAsync().Result);

    var path = SERVER_TEMP_PATH + "\\" + responseDeserialized.Files.First().FileName;
    System.IO.File.WriteAllText(path, responseDeserialized.Files.First().FileData);

    return responseDeserialized.Files.First().FileName;
}

Upvotes: 1

Views: 1798

Answers (2)

Tomas
Tomas

Reputation: 18117

Why to use JSON response in C# when you can use binary response instead. A response will be smaller, no need to decode. To change response type you need to add accept=application/octet-stream header to request to ask for binary response from server. The whole code will look like

using System;
using System.Net;
using System.IO;

class MainClass {
  public static void Main (string[] args) {
            const string fileToConvert = "test.docx";
            const string fileToSave = "test.pdf";           
            const string Secret="";

            if (string.IsNullOrEmpty(Secret))
              Console.WriteLine("The secret is missing, get one for free at https://www.convertapi.com/a");
            else
              try
              {
                  Console.WriteLine("Please wait, converting!");
                  using (var client = new WebClient())
                  {
                      client.Headers.Add("accept", "application/octet-stream");
                      var resultFile = client.UploadFile(new Uri("http://v2.convertapi.com/docx/to/pdf?Secret=" + Secret), fileToConvert); 
                      File.WriteAllBytes(fileToSave, resultFile );
                      Console.WriteLine("File converted successfully");
                  }
              }
              catch (WebException e)
              {
                  Console.WriteLine("Status Code : {0}", ((HttpWebResponse)e.Response).StatusCode);
                  Console.WriteLine("Status Description : {0}", ((HttpWebResponse)e.Response).StatusDescription);
                  Console.WriteLine("Body : {0}", new StreamReader(e.Response.GetResponseStream()).ReadToEnd());
              }
  }
}

Upvotes: 0

Jonas
Jonas

Reputation: 5149

File data in JSON is Base64 encoded, decode it before writing to a file.

public string ConvertReportToPDF(string fileName)
{
    string resultFileName = "";
    key = "xxxxx";

    var requestContent = new MultipartFormDataContent();
    var fileStream = System.IO.File.OpenRead(fileName);
    var stream = new StreamContent(fileStream);
    requestContent.Add(stream, "File", fileStream.Name);

    var response = new HttpClient().PostAsync("https://v2.convertapi.com/docx/to/pdf?Secret=" + key, requestContent).Result;
    FileReportResponse responseDeserialized = JsonConvert.DeserializeObject<FileReportResponse>(response.Content.ReadAsStringAsync().Result);

    var path = SERVER_TEMP_PATH + "\\" + responseDeserialized.Files.First().FileName;
    System.IO.File.WriteAllText(path, Convert.FromBase64String(responseDeserialized.Files.First().FileData));

    return responseDeserialized.Files.First().FileName;
}

Upvotes: 1

Related Questions