I am trying out ApprovalTests.NET to test some (mostly legacy) PDF generation code build on MigraDoc. The code under test renders the PDF to a MemoryStream, which is then scrubbed of various metadata properties (using code I adapted from PdfScrubber
), converted to a byte array and verified using Approvals.VerifyBinaryFile()
.
The tests pass on my machine and that of a colleague (both running Windows 10), but fail on our TeamCity build agent (an Azure VM, running Windows Server 2012 R2 I think). Comparing the Received file (generated on the build server) and the Approved file (generated on my machine), the metadata portions of the files are identical, but the binary portions are totally different, with one file being about 1 kb shorter than the other.
What might be causing the discrepancy? Is it likely to be OS-related?
Edit
The issue appears to be fonts (thanks to PDFSharp Expert for the suggestion). On closer inspection there are two binary objects that differ, and these apparently define the header and body fonts: when I remove one and then the other, the headers and the body text respectively turn to dots.
So, is there a way to guarantee that all machines will produce the same output with regard to fonts? So far I've tried:
- passing
PdfFontEmbedding.None
to the constructor ofPdfDocumentRenderer
(previously it was usingPdfFontEmbedding.Always
) setting a private font like so:
var fonts = new XPrivateFontCollection(); var arial = File.ReadAllBytes("path/to/arial/copied/from/windows/server.ttf"); fonts.AddFont(arial, "Arial"); XPrivateFontCollection.SetGlobalFontCollection(fonts);
In both cases I'm getting the same output as before on my local machine.