0

I have a code which gets byte array from my database and sends it to convertAPI to convert it as PDF.

When I download the doc as is it works fine (so no issue with the byte[] or document), but when I send the stream to ConvertAPI the resultant PDF is all garbled and has more than 200 pages (the doc only has 1 page). the document does contain image and Chinese text.

here is my code (application is written in C# web api):

var convertApi = new ConvertApi("<my key>");
var stream = new MemoryStream(documents[0].content);
var convertToPdf = await convertApi.ConvertAsync("doc", "pdf",
                new ConvertApiFileParam(stream, "test.doc")
            );
var outputStream = await convertToPdf.Files[0].FileStreamAsync();
Tomas
  • 17,551
  • 43
  • 152
  • 257
Karthik Ganesan
  • 4,142
  • 2
  • 26
  • 42

1 Answers1

1

Works fine, try to copy to the stream and set stream position to 0;

            var convertApi = new ConvertApi("secret");
            try
            {
                var stream = new MemoryStream();


                using (var file = new FileStream(@"..\..\..\..\examples\TestFiles\test.docx", FileMode.Open, FileAccess.Read))
                    await file.CopyToAsync(stream);
                stream.Position = 0;

                var convertToPdf = await convertApi.ConvertAsync("docx", "pdf",
                    new ConvertApiFileParam(stream, "test.docx")
                );

                var outputStream = await convertToPdf.Files.First().FileStreamAsync();

                Console.Write(new StreamReader(outputStream).ReadToEnd());
                Console.WriteLine("End of file stream.");
                Console.ReadLine();
            }
            catch (ConvertApiException e)
            {
                Console.WriteLine("Status Code: " + e.StatusCode);
                Console.WriteLine("Response: " + e.Response);
            }
Tomas
  • 17,551
  • 43
  • 152
  • 257