Free Support Forum - aspose.com

Problems in processing multiple simultaneous requests to concatinate large .pdf files

Hi,

While working on a MergePDF application to do the following:

1. Upload two or more .pd files to the server

2. Concatenate file to generate new .pdf

3. Provide the merged file for download

One request works fine. When multiple requests are sent simultaneously to the shared Windows server, some of the requests fail. For example, I sent 16 simultaneous requests. One time, four of them failed. Another time, all succeeded. A third time, fourteen of sixteen failed! Error messages:

* Page not found
* Wrong format in true type font!
* Object reference not set to an instance of an object.
* Safe handle has been closed
* select/poll returned error
* transfer closed with outstanding read data remaining

Please help. Here is the code snippett

protected void Page_Load(object sender, EventArgs e)

{

//Adding a delay of 2 seconds

System.Threading.Thread.Sleep(2000);

Filename = Guid.NewGuid().ToString();

//LogEntry("Footer Text-" + Request.QueryString["text"].ToString());

string dirName = Guid.NewGuid().ToString();

Directory.CreateDirectory(Request.PhysicalApplicationPath + "..\\db\\TempPdf" + "\\" + dirName);

path = Request.PhysicalApplicationPath + "..\\db\\TempPdf" + "\\" + dirName;

LogEntry("1). Page Load");

string OutputFileName = Path.Combine(path, Guid.NewGuid().ToString() + ".pdf");

finalFileName = Path.Combine(path, Filename + ".pdf");

LogEntry("1). All fle names are declaired");

HttpFileCollection httpPostedFiles = Request.Files;

string[] TempFileName = new string[httpPostedFiles.Count];

Aspose.Pdf.Kit.License licencekit = new Aspose.Pdf.Kit.License();

licencekit.SetLicense(Request.PhysicalApplicationPath + "\\" + "Aspose.Total.lic");

LogEntry("First File Name");

LogEntry(httpPostedFiles[0].FileName);

LogEntry("Second File Name");

LogEntry(httpPostedFiles[1].FileName);

LogEntry("Both file names are written");

try

{

for (int Loop1 = 0; Loop1 < httpPostedFiles.Count; Loop1++)

{

// Create a new file name.

TempFileName[Loop1] = Path.Combine(path, Guid.NewGuid().ToString() + ".pdf");

// Save the file.

httpPostedFiles[Loop1].SaveAs(TempFileName[Loop1]);

}

LogEntry("2). The uploaded files are stored at server");

PdfFileEditor pdfEditor = new PdfFileEditor();

pdfEditor.Concatenate(TempFileName, OutputFileName);

LogEntry("3). Merged pdf is formed");

//Response.Clear();

//Response.ContentType = "application/pdf";

//Response.AddHeader("Content-Disposition", "attachment; filename=");

////Response.AddHeader("Content-Length", finalFileName.Length.ToString());

//Response.Flush();

//Response.Write(finalFileName);

//LogEntry("1). Before transmit file");

////Response.TransmitFile(finalFileName);

//LogEntry("1). After transmit file");

//Response.End();

}

catch (Exception ex)

{

string query = ex.Message.ToString();

//query = "Error Message";

Response.Redirect("ErrorPage.aspx?error='" + query + "'");

//Response.End();

//Server.Transfer("ErrorPage.aspx");

//LogEntry(ex.Message);

Label1.Text = ex.Message.ToString();

}

finally

{

//response to the client

//open the finalfile in filestream before delete it

FileStream responseFile = new FileStream(finalFileName, FileMode.Open);

byte[] buffer1 = new byte[Convert.ToInt64(responseFile.Length)];

Response.ContentType = "application/pdf";

Response.Clear();

Response.AddHeader("Content-Disposition", "attachment; filename=");

Response.AddHeader("Content-Length", responseFile.Length.ToString());

//Response.AddHeader("Content-Length",fi.Length.ToString() );

Response.Charset = "UTF-8";

responseFile.Read(buffer1, 0, (int)responseFile.Length);

responseFile.Close();

Response.BinaryWrite(buffer1);

//Response.WriteFile(finalFileName);

//Directory.Delete(path);

string[] files = Directory.GetFiles(path);

foreach (string file in files)

File.Delete(file);

Directory.Delete(path);

Response.End();

}

}

Hi,

Thank you for considering Aspose.

We will investigate this issue and reply to you. Currently we are in holiday so we might reply to you a little later. Sorry for the inconvenience.

How long will you be on leave. I need this resolved ASAP.

We will reply to you early next week.

Hi,

Thank you for considering Aspose.

Afer investigating the problem, I think a possible factor of the error might be the I/O delay in the process of transfering data (from memory to disk) of PdfFileEditor.Concatenate(string, string) method. It's a little like the classical Producer-Consumer problem.

step 1. (Producer) merging pdf files to object .pdf using the Concatenate(): here the object .pdf file is created and the I/O start (writting data from source files to the object file)

step 2. (Consumer) reading or downloading the object .pdf file from a response page: there would be errors if the I/O has not been finished(eg, a big file)

It's difficult to affirm the real factor(s) for the problem of this web application, and I am not sure the above analyse be quite right. Please try the following solution (written in pseudocode, please translate them to your real code) and give us feedback. If it works, then we could add a new flag in the class PdfFileEditor to indicate whether the I/O operation finished or not, to solve the problem.

...

StartMergePDF;
if (new PDF file Found )
{
wait(some time); // here "some time" should be variable according to the PDF file size, or be rather a long time for all possible big files.
Response.redirect(PDFpath);
}

...

Thanks,