2

Bit of a .NET problem here as I am not a .NET programmer. I use Navision :)

I am using Navision to connect to a Webservice that returns a PDF. The code I am using is below.

HttpClient := HttpClient.HttpClient();

SubmitData := 'partnerid=MY1003';
SubmitData += '&password=mc1222';
SubmitData += '&country=DE';
SubmitData += '&firstname=Me';
SubmitData += '&lastname=McMe';

HttpContent := HttpContent.StringContent(SubmitData, Encoding.UTF8, 'application/x-www-form-urlencoded');
HttpResponseMessage := HttpClient.PostAsync(Uri.Uri('https://api-return-pdf.theirwebsite.com/PDFService/V1/getPDF'), HttpContent).Result;

IF (HttpResponseMessage.IsSuccessStatusCode) THEN BEGIN

  ResponseData := HttpResponseMessage.Content.ReadAsStringAsync().Result;
  f.CREATEOUTSTREAM(fileoutstream);
  fileoutstream.WRITETEXT(ResponseData);  

END;

The problem is however that the PDF returned appears to be entirely empty! But if I build a basic web submit form to do this, the PDF returned is perfect.

<form action="https://api-return-pdf.theirwebsite.com/PDFService/V1/getPDF" method="POST">
 <table>
 <thead></thead>
 <tbody>
 <tr>
 <td>User, Pass</td>
 <td><input name="partnerid" type="text">
 <input name="password" type="text">
 </td>
 </tr>
 <tr>
 <td>Vorname, Nachname</td>
 <td><input name="firstname" type="text">
 <input name="lastname" type="text">
 </td>
 </tr>
 <tr>
 <td>country:</td>
 <input name="country" type="de">
 </td>
 </tr>
 </tbody>
 </table>
 <input type="submit" value="Send" />
 <input type="reset" />
</form>

When I edit the resulting PDF in NOTEPAD I see the following at the beginning which LOOKS like a correct PDF to me.....

%PDF-1.4
%????
3 0 obj
<</Type/XObject/ColorSpace/DeviceGray/Subtype/Image/BitsPerComponent 8/Width 227/Length 30/Height 35/Filter/FlateDecode>>stream
x???

So my only guess is that the encoding of the STRING I stream out to the file is somehow wrong............ should there be some form of conversion I perform before/during saving the PDF???

Or are there better .NET classes I could be using to achieve the same effect without this problem?

  • Hi, the `f` variable is `File` and `fileoutstream` is `OutStream` right? `ResponseData` is `Text`? `ResponseData` have the correct value? – Jonathan Bravetti Oct 20 '16 at 15:18
  • f variable is a Navision "File" variable yes. fileoutstream is a Navision OutStream, not a .net one. And ResponseData is indeed a Navision Text variable too. – Nozzferrahhtoo Oct 21 '16 at 06:21

3 Answers3

0

the pdf file does not work because you really are saving a file txt with pdf extension. For this reason when you open this file with notepad looks like OK.

I think you need create a function in .NET or C# to do this. and then invoke from NAV to save this values.

Perhaps something like this:

PdfDocument pdf   = new PdfDocument();
pdf.Info.Title    = "PDF Title";
PdfPage pdfPage   = pdf.AddPage();
XGraphics graph   = XGraphics.FromPdfPage(pdfPage);
XFont font = new XFont("Verdana", 11, XFontStyle.Bold);
graph.DrawString("Your PDF Line", font, XBrushes.Black, new XRect(0, 0, pdfPage.Width.Point, pdfPage.Height.Point), XStringFormats.Center);
string pdfFilename = "FileName.pdf";
pdf.Save(pdfFilename);
Process.Start(pdfFilename);

Using this references:

using System;
using System.Diagnostics;
using PdfSharp;
using PdfSharp.Drawing;
using PdfSharp.Pdf;

Or Perhaps you can download the whole pdf file (not read) and save in your location.

I hope it helps you.

Jonathan Bravetti
  • 2,228
  • 2
  • 15
  • 29
0

Thanks for that. Alas I have no idea how to program in .NET or what coding environments to use or anything :)

So I was kinda hoping someone here would either know:

1) A better way to download the file using existing .NET libraries or

2) Using existing .NET libraries how to augment the way I am currently downloading the file in order to save it correctly as PDF and not, as you say, as TXT :)

0

Ok all, sorry if I wasted any of your times. But I solved it myself. I know HOW I solved it, I just do not know WHY it worked.

Instead of reading the HTTPResponse into a string, I read it into a .NET array. Then I created a MemoryStream of that Array.

From there I streamed the result into a Navision "BLOB" field in the database and then used the internal Blob.EXPORT('C:\temp\result.pdf') command to export the file from that blob. And the result is a perfect PDF.

I guess somewhere along the way there is a minor encoding difference, but how, why or what is outside my pay grade I guess :)