1

我正在使用 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;
}
4

2 回答 2

1

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;
}
于 2018-02-08T07:48:52.120 回答
0

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

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());
              }
  }
}
于 2018-02-08T08:30:48.250 回答