Kubernetes - Throws error when saving pdf to stream

I’m running Aspose.Pdf 22.5.0 with .Net Core 3.1 (Alpine) in Kubernetes.

I can create a Document object from a byte array in a Kubernetes deployment but if I try to save back to a stream then it throws an error. This code works if I run it locally in Windows but not to a deployed Linux container in Kubernetes.

I found several messages related to this from a year ago but no resolutions. I can post an example but I’m not sure it’ll add anything not already discussed before.

Are there any resolutions for this yet?

I link to libgdiplus in my DockerFile and even implemented the runtimeconfig.template.json file with “System.Drawing.EnableUnixSupport”: true.

var isValidPdf = true;
using ( MemoryStream stream = new MemoryStream( pdfBytes ) )
{
	PdfFileInfo info = new PdfFileInfo( stream );
	isValidPdf = info.IsPdfFile; // Check that pdf has valid contents
	if ( isValidPdf )
	{
		var pdfFile = new Document( stream );
		pages = pdfFile.Pages.Count;
		// The below code works locally in Windows but not in Kubernetes
		if ( pdfBytes.Length > _maxPdfSizeKb || pdfFile.PdfFormat == PdfFormat.v_1_1 )
		{
			using ( MemoryStream stream2 = new MemoryStream() )
			{
				if ( pdfFile.PdfFormat == PdfFormat.v_1_1 )
				{
					pdfFile.Convert( new MemoryStream(), PdfFormat.v_1_7, ConvertErrorAction.Delete );
				}

				if ( pdfBytes.Length > _maxPdfSizeKb )
				{
					var optimizeOptions = new Aspose.Pdf.Optimization.OptimizationOptions();
					optimizeOptions.ImageCompressionOptions.CompressImages = true;
					optimizeOptions.ImageCompressionOptions.ImageQuality = 50;
					pdfFile.OptimizeResources( optimizeOptions );
				}

				pdfFile.Save( stream2, SaveFormat.Pdf ); // *** This is the line which throws the exception
				pdfBytes = stream2.ToArray();
			}
		}
	}
}

My Dockerfile contains libgdiplus

FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-alpine
RUN echo "http://dl-cdn.alpinelinux.org/alpine/edge/testing" >> /etc/apk/repositories && apk update && apk add --no-cache libgdiplus
WORKDIR /app
ENV ASPNETCORE_URLS=http://+:5000
RUN mkdir /certs
EXPOSE 5000

Here are the errors:

2022-05-24T14:30:47.976880365-06:00       Process file: Object reference not set to an instance of an object.
2022-05-24T14:30:47.976887965-06:00          at #=z662oIzUf2eLF0THZFMN8WoI6gucL.#=zG6kKQtE=(#=z17j7UePZW_R9dnAOFNnupOutUt8G #=zuAJa3PlpbVUm)
2022-05-24T14:30:47.976894160-06:00          at #=zpF4W6IJ_fPnG2v1O6s0xbxg=.#=zy4n_pFI=(#=z17j7UePZW_R9dnAOFNnupOutUt8G #=zuAJa3PlpbVUm)
2022-05-24T14:30:47.976899468-06:00          at #=quvML5pSpoSylFJjPfPEz1mtmAiuVkDTYLSvyEoNCTXs=.#=z6qnqYaIRilrcmwxRg0bFVykc4XkeGgwTDu54MkQ=(Object #=zqP3HOoA=)
2022-05-24T14:30:47.976905199-06:00          at #=quvML5pSpoSylFJjPfPEz1mtmAiuVkDTYLSvyEoNCTXs=.#=zOuAof17LzJyRhqb_zQXuHbZEqB6$wLcLRDxGMws=(MethodBase #=zqP3HOoA=, Boolean #=zYkx$yWY=)
2022-05-24T14:30:47.976911392-06:00          at #=quvML5pSpoSylFJjPfPEz1mtmAiuVkDTYLSvyEoNCTXs=.#=zsegKOyJJDnlA_MxpdVm3moU=(#=quvML5pSpoSylFJjPfPEz1mtmAiuVkDTYLSvyEoNCTXs= #=zqP3HOoA=, #=qpbTRMLH7xUZ54svv4oMlLIKclv4Cvn7lwKzzb0de$8U= #=zYkx$yWY=)
2022-05-24T14:30:47.976917114-06:00          at #=quvML5pSpoSylFJjPfPEz1mtmAiuVkDTYLSvyEoNCTXs=.#=zrWG22eSlM9AThdy8W6OIBMXml$f4SLtkUg==()
2022-05-24T14:30:47.976922630-06:00          at #=quvML5pSpoSylFJjPfPEz1mtmAiuVkDTYLSvyEoNCTXs=.#=zLP2kdZ5lpXRW5yFDd65_BshiGEehyIishA==(Boolean #=zqP3HOoA=)
2022-05-24T14:30:47.976933519-06:00          at #=quvML5pSpoSylFJjPfPEz1mtmAiuVkDTYLSvyEoNCTXs=.#=z6qnqYaIRilrcmwxRg0bFVykc4XkeGgwTDu54MkQ=(Object #=zqP3HOoA=)
2022-05-24T14:30:47.976938532-06:00          at #=quvML5pSpoSylFJjPfPEz1mtmAiuVkDTYLSvyEoNCTXs=.#=zXVqHsvphj9y5CDivOGDmCepnEib2vYO$TGTsu_RNm7yE()
2022-05-24T14:30:47.976944029-06:00          at #=quvML5pSpoSylFJjPfPEz1mtmAiuVkDTYLSvyEoNCTXs=.#=z5mPazZNYdcAMdadhVxzl42hG7klu1sRyHw==(#=quvML5pSpoSylFJjPfPEz1mtmAiuVkDTYLSvyEoNCTXs= #=zqP3HOoA=, #=qpbTRMLH7xUZ54svv4oMlLIKclv4Cvn7lwKzzb0de$8U= #=zYkx$yWY=)
2022-05-24T14:30:47.976952895-06:00          at #=quvML5pSpoSylFJjPfPEz1mtmAiuVkDTYLSvyEoNCTXs=.#=zrWG22eSlM9AThdy8W6OIBMXml$f4SLtkUg==()
2022-05-24T14:30:47.977016958-06:00          at #=quvML5pSpoSylFJjPfPEz1mtmAiuVkDTYLSvyEoNCTXs=.#=zLP2kdZ5lpXRW5yFDd65_BshiGEehyIishA==(Boolean #=zqP3HOoA=)
2022-05-24T14:30:47.977025986-06:00          at #=quvML5pSpoSylFJjPfPEz1mtmAiuVkDTYLSvyEoNCTXs=.#=z6qnqYaIRilrcmwxRg0bFVykc4XkeGgwTDu54MkQ=(Object #=zqP3HOoA=)
2022-05-24T14:30:47.977031363-06:00          at #=quvML5pSpoSylFJjPfPEz1mtmAiuVkDTYLSvyEoNCTXs=.#=zXVqHsvphj9y5CDivOGDmCepnEib2vYO$TGTsu_RNm7yE()
2022-05-24T14:30:47.977036294-06:00          at #=quvML5pSpoSylFJjPfPEz1mtmAiuVkDTYLSvyEoNCTXs=.#=zUj0LFnNijhVjQHFM5lXWpyXtdKrh(Object #=zqP3HOoA=, UInt32 #=zYkx$yWY=)
2022-05-24T14:30:47.977047472-06:00          at #=quvML5pSpoSylFJjPfPEz1mtmAiuVkDTYLSvyEoNCTXs=.#=zLP2kdZ5lpXRW5yFDd65_BshiGEehyIishA==(Boolean #=zqP3HOoA=)
2022-05-24T14:30:47.977052081-06:00          at #=quvML5pSpoSylFJjPfPEz1mtmAiuVkDTYLSvyEoNCTXs=.#=zLP2kdZ5lpXRW5yFDd65_BshiGEehyIishA==(Boolean #=zqP3HOoA=)
2022-05-24T14:30:47.977056420-06:00          at #=quvML5pSpoSylFJjPfPEz1mtmAiuVkDTYLSvyEoNCTXs=.#=zmugJb9WhyKAt2RmOEcxFJuG5fx$x5pCGN6Ka3CY=()
2022-05-24T14:30:47.977061501-06:00          at #=quvML5pSpoSylFJjPfPEz1mtmAiuVkDTYLSvyEoNCTXs=.#=zMARaN9C52_$IWNX9wjGjxWhjK8odOLFYObgEb4g=(Object[] #=zqP3HOoA=, Type[] #=zYkx$yWY=, Type[] #=zJULsBOs=, Object[] #=zgPaMa5o=)
2022-05-24T14:30:47.977066577-06:00          at #=quvML5pSpoSylFJjPfPEz1mtmAiuVkDTYLSvyEoNCTXs=.#=zPCnY2S2FicVyH9Z4sFsPUmmxs4sPa3Z6PEdKW0E=(Stream #=zqP3HOoA=, String #=zYkx$yWY=, Object[] #=zJULsBOs=, Type[] #=zgPaMa5o=, Type[] #=zKfw4XYQ=, Object[] #=z2aXqa7I=)
2022-05-24T14:30:47.977071499-06:00          at #=quvML5pSpoSylFJjPfPEz1mtmAiuVkDTYLSvyEoNCTXs=.#=zp8HNU$53r7X0as0Z$lRLdu3uBGrvn_kutg==(Stream #=zqP3HOoA=, String #=zYkx$yWY=, Object[] #=zJULsBOs=)
2022-05-24T14:30:47.977076634-06:00          at #=quvML5pSpoSylFJjPfPEz1mtmAiuVkDTYLSvyEoNCTXs=.#=zmYbqDCT9qzs0WK_NkOYtTNMzI8XaRAfox4lZzeo=(Stream #=zqP3HOoA=, String #=zYkx$yWY=, Object[] #=zJULsBOs=)
2022-05-24T14:30:47.977082180-06:00          at Aspose.Pdf.Document.#=zd39Ywf_1G7g7(Stream #=zfpx$MTw=, SaveOptions #=zvSsgIEJLOrv1)
2022-05-24T14:30:47.977087121-06:00          at Aspose.Pdf.Document.#=zlrMiLgwkESst(Stream #=z8GA1ZHyjHTLi, SaveOptions #=zKmkgzZM=)
2022-05-24T14:30:47.977091851-06:00          at Aspose.Pdf.Document.#=zlrMiLgwkESst(Stream #=z8GA1ZHyjHTLi, SaveFormat #=zY3xs_VU=)

@williamfa

Could you please ZIP and attach your input document along with Visual Studio project to reproduce the same issue at our end? We will investigate the issue and provide you more information on it.

I’m afraid that isn’t possible; there are too many dependencies on other projects as well as proprietary information. This code works fine running under windows and only throws the error when running in a Kubernetes Linux container.

Were there resolutions to the other tickets with similar issues?

@williamfa

Please share the working code example and input PDF here for testing. We will create the same Docker application and test this case. We will share our findings with you after testing.

Please see my previous response. The entire K8S microservice project would be useless to you without our infrastructure. Any, absolutely any, pdf file will produce the same result with the included code from the first post if it executes in k8s in a Linux container. It just uses a byte array from a pdf.

@williamfa

We have logged this problem in our issue tracking system. We will inform you via this forum thread once there is an update available on it.

We apologize for your inconvenience.