We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

PDF to image - corrupted image when used concurrently

I am trying to use the PDF-to-image converter in a multi-threaded environment. I am using the following code to render the first page of a PDF:

static void Render(Document document, int iteration)
{
var converter = new PdfConverter(document)
{
StartPage = 1,
EndPage = 1,
Resolution = new Resolution(150)
};
// Convert
converter.DoConvert();

// Sanity check
if (!converter.HasNextImage())
    throw new InvalidOperationException("Failed to render the page");

var outputFilename = Path.ChangeExtension(Path.GetRandomFileName(), ".png");
using (var fs = new FileStream(outputFilename, FileMode.Create, FileAccess.Write))
{
    converter.GetNextImage(fs, ImageFormat.Png);
}

// Compute the MD5 hash, it should not change
var hash = MD5.Create().ComputeHash(File.ReadAllBytes(outputFilename));
Console.WriteLine("Iteration: {0:D4}\tFile: {1}\tHash: {2}", iteration, outputFilename, ArrayToString(hash));

}

static void Render(byte[] fileData, int iteration)
{
using (var ms = new MemoryStream(fileData, writable: false))
using (var doc = new Document(ms))
{
// Based on testing I have found that Aspose.Pdf.Document
// is not safe to use in multiple threads concurrently,
// so create a new instance every time
Render(doc, iteration);
}
}

static string ArrayToString(byte[] bytes)
{
return string.Join(string.Empty, bytes.Select(b => b.ToString(“X”)));
}

Here is my driver code. When I use more than 1 thread, some images are corrupt.

const int NumIterations = 40;
const int NumThreads = 4;

var options = new ParallelOptions { MaxDegreeOfParallelism = NumThreads };
var bytes = File.ReadAllBytes(FilePath);
Parallel.For(0, NumIterations, options, (i, s) => Render(bytes, i));

Here is some sample output. The 2 images with different MD5 hashes (lines 2 and 3) are missing text, or have incorrect fonts, etc.

Iteration: 0020	File: corjhfgj.png	Hash: FE7B49CDB624ED119467DD1F45E18CC 
Iteration: 0000	File: c4kumti5.png	Hash: B08FDE16985777AA36E7B9DEAC92E3C 
Iteration: 0010	File: z11nrffe.png	Hash: 3AB3C123A9CFD19EDD80D2668F6B229E 
Iteration: 0030	File: tn4mcutc.png	Hash: FE7B49CDB624ED119467DD1F45E18CC 
Iteration: 0001	File: aoj05lex.png	Hash: FE7B49CDB624ED119467DD1F45E18CC 
Iteration: 0011	File: lczc3gfb.png	Hash: FE7B49CDB624ED119467DD1F45E18CC 
Iteration: 0021	File: frldyken.png	Hash: FE7B49CDB624ED119467DD1F45E18CC 
Iteration: 0031	File: rw0kwrb1.png	Hash: FE7B49CDB624ED119467DD1F45E18CC 
Iteration: 0012	File: f5zljdc0.png	Hash: FE7B49CDB624ED119467DD1F45E18CC 
Iteration: 0022	File: vhdfsof1.png	Hash: FE7B49CDB624ED119467DD1F45E18CC 
Iteration: 0002	File: dwmljjhq.png	Hash: FE7B49CDB624ED119467DD1F45E18CC 
Iteration: 0032	File: 3dvpw02u.png	Hash: FE7B49CDB624ED119467DD1F45E18CC 
Iteration: 0013	File: gpfktajf.png	Hash: FE7B49CDB624ED119467DD1F45E18CC 
Iteration: 0023	File: bp0g1fyd.png	Hash: FE7B49CDB624ED119467DD1F45E18CC 
Iteration: 0003	File: bqzmeq01.png	Hash: FE7B49CDB624ED119467DD1F45E18CC 
Iteration: 0033	File: 0vixceuw.png	Hash: FE7B49CDB624ED119467DD1F45E18CC 
Iteration: 0004	File: lds2x4s2.png	Hash: FE7B49CDB624ED119467DD1F45E18CC 
Iteration: 0014	File: eybm3vxs.png	Hash: FE7B49CDB624ED119467DD1F45E18CC 
Iteration: 0024	File: ipnxi1hw.png	Hash: FE7B49CDB624ED119467DD1F45E18CC 
Iteration: 0034	File: 4rmhllmj.png	Hash: FE7B49CDB624ED119467DD1F45E18CC 
Iteration: 0005	File: ngakuss2.png	Hash: FE7B49CDB624ED119467DD1F45E18CC 
Iteration: 0015	File: 4d4rjgld.png	Hash: FE7B49CDB624ED119467DD1F45E18CC 
Iteration: 0025	File: j0kweccd.png	Hash: FE7B49CDB624ED119467DD1F45E18CC 
Iteration: 0035	File: ugdudllf.png	Hash: FE7B49CDB624ED119467DD1F45E18CC 
Iteration: 0006	File: fgubkcbg.png	Hash: FE7B49CDB624ED119467DD1F45E18CC 
Iteration: 0016	File: ox2ngbax.png	Hash: FE7B49CDB624ED119467DD1F45E18CC 
Iteration: 0026	File: jqqybucc.png	Hash: FE7B49CDB624ED119467DD1F45E18CC 
Iteration: 0036	File: mmvibdmu.png	Hash: FE7B49CDB624ED119467DD1F45E18CC 
Iteration: 0007	File: e4qoujd4.png	Hash: FE7B49CDB624ED119467DD1F45E18CC 
Iteration: 0017	File: jfn2otmf.png	Hash: FE7B49CDB624ED119467DD1F45E18CC 
Iteration: 0027	File: olv3jiu0.png	Hash: FE7B49CDB624ED119467DD1F45E18CC 
Iteration: 0008	File: leh5zwod.png	Hash: FE7B49CDB624ED119467DD1F45E18CC 
Iteration: 0037	File: os2hzouz.png	Hash: FE7B49CDB624ED119467DD1F45E18CC 
Iteration: 0018	File: rzapb2to.png	Hash: FE7B49CDB624ED119467DD1F45E18CC 
Iteration: 0038	File: ia244wys.png	Hash: FE7B49CDB624ED119467DD1F45E18CC 
Iteration: 0028	File: knsbv0mg.png	Hash: FE7B49CDB624ED119467DD1F45E18CC 
Iteration: 0019	File: vzdnshf1.png	Hash: FE7B49CDB624ED119467DD1F45E18CC 
Iteration: 0009	File: 5tlvvxm0.png	Hash: FE7B49CDB624ED119467DD1F45E18CC 
Iteration: 0039	File: k2apxqlx.png	Hash: FE7B49CDB624ED119467DD1F45E18CC 
Iteration: 0029	File: cwlczq1r.png	Hash: FE7B49CDB624ED119467DD1F45E18CC 

You may have to run it a few times to see the problem. I am testing on a machine with 2 CPU cores. Because it is always the first few images that are corrupt, I would guess that you are doing some initialization that is not thread safe.

I will attach the PDF I'm using and sample good/bad outputs.

Hi S4i,

Thank you for using our products and sharing the sample source code and template documents with us. I managed to reproduce this issue on my side. For the sake of correction, I have logged this issue as PDFNEWNET-33355 in our bug tracking system. Your request has also linked to the appropriate issue and you will be notified as soon as it is resolved.

We apologize for your inconvenience.

Thanks & Regards,

I am no longer seeing this bug in 7.2. Can you confirm that this was fixed?

Hi,


Thanks for your patience.

In recent release version, we have fixed many issues related to Pdf to image conversion so I think this particular issue might have been fixed as a result of correct to those issues. Please continue using the new release version and in case you encounter the similar problem or you have any further query, please feel free to contact.

The issues you have found earlier (filed as PDFNEWNET-33355) have been fixed in Aspose.Pdf for .NET 7.5.0.


This message was posted using Notification2Forum from Downloads module by aspose.notifier.