I recently upgraded my AWS Lambda function from .NET Core 3.1 to .NET 8.0, and after the upgrade, I encountered an issue where images are no longer being appended in the Publisher files when converting to PDF. Text conversion to PDF works as expected, but the images are missing in the resulting document. Since System.Drawing is not supported in .NET 8 in linux environment I am using Aspose.Drawing package.
Since this is business critical issue, I will require your assistance promptly.
Here’s the original code that was working fine prior to the upgrade:
var pubDoc = PubFactory.CreateParser(inStream);
var pubDocToPdf = pubDoc.Parse();
PubFactory.CreatePdfConverter().ConvertToPdf(pubDocToPdf, outStream);
Aspose.Pdf.Document pubPdfDoc = new Aspose.Pdf.Document(outStream);
After the upgrade, I have tried the following different code approaches, but none of them solve the problem of missing images in the final PDF output:
First Attempt - Image Optimization
case “.pub”:
Console.WriteLine(“Converting Publisher document with image optimization…”);
inStream.Position = 0;
// First stage: Convert to PDF using Aspose.Pub
var pubDoc = PubFactory.CreateParser(inStream);
var pubDocToPdf = pubDoc.Parse();
using (var tempStream = new MemoryStream())
{
// Initial conversion
PubFactory.CreatePdfConverter().ConvertToPdf(pubDocToPdf, tempStream);
tempStream.Position = 0;
// Second stage: Optimize with Aspose.Pdf
using (var pdfDoc = new Aspose.Pdf.Document(tempStream))
{
// Configure PDF options for better image handling
var pdfDeviceInfo = new Aspose.Pdf.Devices.PdfDeviceInfo
{
PageSize = new Aspose.Pdf.Devices.PageSize(800, 600),
Quality = 100
};
// Set rendering options
pdfDoc.PageInfo.Width = 800;
pdfDoc.PageInfo.Height = 600;
pdfDoc.OptimizeResources();
// Save with optimization
pdfDoc.Save(outStream, Aspose.Pdf.SaveFormat.Pdf);
pageCountDocument = pdfDoc.Pages.Count.ToString();
}
}
break;
Second Attempt - Enhanced Rendering
case “.pub”:
Console.WriteLine(“Converting Publisher document with enhanced rendering…”);
inStream.Position = 0;
// Initial conversion
var pubDoc = PubFactory.CreateParser(inStream);
var pubDocToPdf = pubDoc.Parse();
using (var tempStream = new MemoryStream())
{
// Convert to PDF first
PubFactory.CreatePdfConverter().ConvertToPdf(pubDocToPdf, tempStream);
tempStream.Position = 0;
// Load into PDF document
using (var pdfDoc = new Aspose.Pdf.Document(tempStream))
{
// Configure save options
var saveOptions = new Aspose.Pdf.PdfSaveOptions
{
EnableLinearization = true,
CompressImages = true,
ImageQuality = 100,
OptimizeResources = true
};
// Process each page to ensure proper image rendering
foreach (var page in pdfDoc.Pages)
{
page.ResizeContent(800, 600, true, true);
page.OptimizeResources();
}
// Save with optimization
pdfDoc.Save(outStream, saveOptions);
pageCountDocument = pdfDoc.Pages.Count.ToString();
}
}
break;
Third Attempt - Fallback Options
case “.pub”:
Console.WriteLine(“Converting Publisher document with fallback options…”);
inStream.Position = 0;
try
{
// Attempt primary conversion with image optimization
var pubDoc = PubFactory.CreateParser(inStream);
var pubDocToPdf = pubDoc.Parse();
using (var tempStream = new MemoryStream())
{
// First attempt with Aspose.Pub
PubFactory.CreatePdfConverter().ConvertToPdf(pubDocToPdf, tempStream);
tempStream.Position = 0;
// Check if the PDF has content
using (var pdfDoc = new Aspose.Pdf.Document(tempStream))
{
if (pdfDoc.Pages.Count > 0)
{
// Configure save options for better image handling
var saveOptions = new Aspose.Pdf.PdfSaveOptions
{
EnableLinearization = true,
CompressImages = true,
ImageQuality = 100
};
pdfDoc.Save(outStream, saveOptions);
pageCountDocument = pdfDoc.Pages.Count.ToString();
}
else
{
throw new Exception("Empty PDF generated");
}
}
}
}
catch (Exception pubEx)
{
Console.WriteLine($"Warning: Primary conversion failed, attempting fallback: {pubEx.Message}");
// Fallback: Create a PDF with warning
using (var doc = new Aspose.Words.Document())
{
var builder = new Aspose.Words.DocumentBuilder(doc);
builder.ParagraphFormat.Alignment = Aspose.Words.ParagraphAlignment.Center;
builder.Writeln("Publisher Document Conversion Notice");
builder.Writeln();
builder.Writeln("This document contains images that could not be fully rendered.");
builder.Writeln("Please try viewing the original file or converting on a Windows system.");
builder.Writeln();
builder.Writeln($"File size: {inStream.Length} bytes");
doc.Save(outStream, Aspose.Words.SaveFormat.Pdf);
pageCountDocument = "1";
}
}
break;
Let me know where I should change so PUB can be converted successfully.
It appears that after upgrading to .NET 8.0, you are encountering issues with images not being included in the PDF output when converting Publisher files. Since you are utilizing the Aspose.Drawing package due to the absence of System.Drawing support in .NET 8 on Linux, let’s focus on ensuring that images are processed correctly during the conversion.
Here are several suggestions to troubleshoot and potentially resolve the issue:
Check Image Formats: Verify that the images in your Publisher document are in a supported format. Unsupported image formats may lead to them being omitted during conversion.
Update Aspose.PUB: Ensure you are using the latest version of Aspose.PUB, as updates may include bug fixes or improvements related to image handling.
Use Temporary Files: Instead of using MemoryStream, consider saving the intermediate PDF to a temporary file. This can help with resource management and ensure that images are processed correctly.
Debugging: Implement logging to check if the images are being parsed correctly from the Publisher document. You can log the number of images detected before conversion.
Fallback to Previous Version: If the issue persists and is critical, consider temporarily reverting to the previous version of .NET or Aspose.PUB that was functioning correctly until a fix is available.
Here’s a modified version of your code that saves the intermediate PDF to a temporary file:
case ".pub":
Console.WriteLine("Converting Publisher document with image handling...");
inStream.Position = 0;
var pubDoc = PubFactory.CreateParser(inStream);
var pubDocToPdf = pubDoc.Parse();
// Use a temporary file for the intermediate PDF
string tempFilePath = Path.GetTempFileName();
try
{
PubFactory.CreatePdfConverter().ConvertToPdf(pubDocToPdf, tempFilePath);
using (var pdfDoc = new Aspose.Pdf.Document(tempFilePath))
{
// Configure save options
var saveOptions = new Aspose.Pdf.PdfSaveOptions
{
EnableLinearization = true,
CompressImages = true,
ImageQuality = 100
};
pdfDoc.Save(outStream, saveOptions);
pageCountDocument = pdfDoc.Pages.Count.ToString();
}
}
finally
{
// Clean up the temporary file
if (File.Exists(tempFilePath))
{
File.Delete(tempFilePath);
}
}
break;
This approach may help ensure that images are processed correctly. If the problem continues, consider reaching out to Aspose support for more specific guidance related to your environment and the version you are using.
Since I’m hosting this as an AWS Lambda function, there’s no temp folder available. Right now, I’m using a MemoryStream. Instead of using a temporary file, would saving the file to a temporary stream work? The goal is to return the PDF content as a MemoryStream, so I’m wondering how it would differ from the original approach.
We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.
Issue ID(s): PUBNET-438
You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.
These issues are under the phase of investigation and we are actively working on improving API features. We are afraid that we do not have any promising ETA at the moment. However, the ticket has already been logged for your specific issues and as soon as it is completely analyzed, we will share updates with you. We request for your patience and comprehension in this regard.
@asad.ali I’m trying to convert a .pub file to .jpg or .bmp, but both conversions produce the same output. Below is the code I’m using — if you have an alternative solution, please let me know.
Can you please share what kind of issue is there in the exported images? We have tested using the file that you shared earlier and obtained result like attached one. Can you please further explain for our reference? output.jpg (136.8 KB)
I am using .NET 8 in a Linux environment (Amazon Linux 2023). When exporting the output from Windows, the result includes images as expected. However, performing the same conversion on Amazon Linux 2023 only renders the text—images are not included in the output.
The issue related to missing images in the output format has already been registered under the ticket ID shared earlier. We have updated the issue description by including latest information shared by you. We will investigate the ticket and keep you posted with the status of its rectification. Please be patient and spare us some time.
I noticed that the Aspose.PUB is still referencing System.Drawing.Common, Version=7.0.0.0
hence rendering in Windows environment is successful.
Here is log from assembly load event when we convert from pub to different formats
We have taken this information into our account and logged it with the ticket. We will include it in our investigation process as well and let you know as soon as we have some feedback. Please spare us some time.