将 API、新 API 版本的 docx 转换为 PDF

Convert API, new API version docx to PDF

我正在使用 Convert API 将 docx 转换为 PDF。使用旧的 API 版本一切正常,但我正在尝试迁移到新的 API 版本,当我打开 PDF 时,它不是有效文档,因此无法打开。不确定我做错了什么,也许与编码有关?

我从 Convert API 得到的响应是 JSON,其中包含文件名、文件大小和文件数据。也许需要处理此文件数据以创建有效的 PDF 文件?如果我只是将该数据写入文件,则它不起作用。

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;
}

JSON 中的文件数据是 Base64 编码的,在写入文件之前对其进行解码。

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;
}

当您可以使用二进制响应时,为什么要在 C# 中使用 JSON 响应。响应会更小,不需要解码。要更改响应类型,您需要添加 accept=application/octet-stream header 以请求从服务器请求二进制响应。整个代码看起来像

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());
              }
  }
}