You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@pdfbox.apache.org by Evan Stansel <ec...@gmail.com> on 2022/09/30 23:14:18 UTC

Issue with PDFBox 1.8.15

Hi,

I have scripts which compare PDF files which have been working for awhile,
but have suddenly stopped. The script converts each page in a PDF into
images which it then compares. I've noticed that this script is now
converting all PDFs into blank images. I have upgraded Java recently, but I
see the same behavior after reverting my Java version. I've tried Java 8,
Java 17 SDK, and Microsoft OpenJDK 17.

I've provided the code being used to compare the PDFs to the bottom of this
email.

As a side note, I've attempted to upgrade PDFBox to the latest version in
the past but I've gotten stuck on some of the migration steps. I could
definitely use some advice in how to convert this script to work with the
latest .jar files.

I also want to mention that I'm not a developer myself. I inherited this
code and I am tasked with getting it working again. I can follow most
javascript enough for scripting but I am very much a novice.

Thank you to anyone that can help me here.
Evan

========================CODE=======================
function ComparePDFs(baseline, output, maskImg)
{
  try
  {
var docObj_1, docObj_2, totalPages_1, totalPages_2, result;
totalPages_1 = getTotalPages(baseline);
totalPages_2 = getTotalPages(output);
for (i = 0; i < totalPages_1; i++)
{
 pic_1 = convertPageToPicture(baseline, i);
 pic_2 = convertPageToPicture(output, i);
 // Compare two images
 if (!pic_1.Compare(pic_2, false, 5, false, 5, maskImg))
 {
// If the images are different, post image differences to the log
Log.Picture(pic_2, "Output Picture", "The output was found at: " + output +
".");
Log.Picture(pic_1, "Baseline Picture", "The baseline can be found at " +
baseline + ".");
Log.Picture(pic_1.Difference(pic_2, false, 5, false, 5, maskImg),
"Comparison Result Image", "The baseline can be found at: " + baseline +
".");
// Post an error message
Log.Error("The PDFs are NOT equal.");
delete pic_1;
delete pic_2;
break;
 }
// Post a message that the pages are equal
Log.Picture(pic_2, "Output Picture", "The output was found at: " + output +
".");
Log.Picture(pic_1, "Baseline Picture", "The baseline can be found at " +
baseline + ".");
Log.Picture(pic_1.Difference(pic_2, false, 5, false, 5, maskImg),
"Comparison Result Image", "The baseline can be found at: " + baseline +
".");
Log.Checkpoint("The PDF Pages are equal.", "The baseline can be found at: "
+ baseline + ".");
}
  }
  catch(e)
  {
Log.Error(e);
Log.Error("There was an error comparing the PDFs.");
  }
}

function getTotalPages(pdfFile)
{
  var docObj =
JavaClasses.org_apache_pdfbox_pdmodel.PDDocument.load_3(pdfFile);
  totalPages = docObj.getNumberOfPages();
  docObj.close();
  return totalPages;
}

function convertPageToPicture(pdfFile, pageIndex)
{
  var pageObj, imgBuffer, imgFile, imgFormat, pictureObj, fileName;

  fileName = "C:\\Users\\" + Sys.UserName +
"\\AppData\\Local\\Temp\\tempImage.jpg";

  // Get the desired page
  pageObj = getPage(pdfFile, pageIndex);

  try
  {
    imgBuffer = pageObj.convertToImage();
  }
  catch(e)
  {
    Log.Error(e);
    Log.Error("Unable to convert the file " + pdfFile + " into an image.
ABORT!");
  }
  if (pageObj.getResources() != null)
  {
    pageObj.getResources().Clear();
  }

  // Create a new file to save
  imgFile = JavaClasses.java_io.File.newInstance(fileName);

  // Get the image format from the name
  imgFormat = aqString.SubString(fileName, aqString.GetLength(fileName)-3,
3);

  // Save the image to the created file
  try
  {
    JavaClasses.javax_imageio.ImageIO.write(imgBuffer, "jpg", imgFile);
  }
  catch(e)
  {
    Log.Error(e);
    Log.Error("Unable to write the file to " + fileName + ".");
  }

  // Create a Picture object
  pictureObj = Utils.Picture;

  // Load the image as a picture
  pictureObj.LoadFromFile(fileName);
  aqFileSystem.DeleteFile(fileName);

  imgBuffer.Flush();
  return pictureObj;
}

ComparePDFs(pdf1.pdf, pdf2.pdf);
=================================================

Re: Issue with PDFBox 1.8.15

Posted by Tilman Hausherr <TH...@t-online.de>.
On 01.10.2022 05:45, Tilman Hausherr wrote:
> re your installation, make sure that there is a pdfbox*.jar and 
> fontbox*.jar of the same version. 

I forgot to mention, using the pdfbox-app jar also works because it 
contains fontbox.

Tilman




---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: users-help@pdfbox.apache.org


Re: Issue with PDFBox 1.8.15

Posted by Tilman Hausherr <TH...@t-online.de>.
Your language isn't java so I googled "Utils.Picture". Apparently you're 
using "TestComplete" from SmartBear which has a java interface.

Try this: download pdfbox-app

https://www.apache.org/dyn/closer.lua/pdfbox/1.8.17/pdfbox-app-1.8.17.jar

then run it with your PDF like this from a command line:

java -jar pdfbox-app-1.8.17.jar PDFToImage <yourfile.pdf>

a jpeg file of your PDF will appear in the directory of the pdf file. Is 
it blank? Do you get any log output?

re your installation, make sure that there is a pdfbox*.jar and 
fontbox*.jar of the same version.

Tilman

On 01.10.2022 01:14, Evan Stansel wrote:
> Hi,
>
> I have scripts which compare PDF files which have been working for awhile,
> but have suddenly stopped. The script converts each page in a PDF into
> images which it then compares. I've noticed that this script is now
> converting all PDFs into blank images. I have upgraded Java recently, but I
> see the same behavior after reverting my Java version. I've tried Java 8,
> Java 17 SDK, and Microsoft OpenJDK 17.
>
> I've provided the code being used to compare the PDFs to the bottom of this
> email.
>
> As a side note, I've attempted to upgrade PDFBox to the latest version in
> the past but I've gotten stuck on some of the migration steps. I could
> definitely use some advice in how to convert this script to work with the
> latest .jar files.
>
> I also want to mention that I'm not a developer myself. I inherited this
> code and I am tasked with getting it working again. I can follow most
> javascript enough for scripting but I am very much a novice.
>
> Thank you to anyone that can help me here.
> Evan
>
> ========================CODE=======================
> function ComparePDFs(baseline, output, maskImg)
> {
>    try
>    {
> var docObj_1, docObj_2, totalPages_1, totalPages_2, result;
> totalPages_1 = getTotalPages(baseline);
> totalPages_2 = getTotalPages(output);
> for (i = 0; i < totalPages_1; i++)
> {
>   pic_1 = convertPageToPicture(baseline, i);
>   pic_2 = convertPageToPicture(output, i);
>   // Compare two images
>   if (!pic_1.Compare(pic_2, false, 5, false, 5, maskImg))
>   {
> // If the images are different, post image differences to the log
> Log.Picture(pic_2, "Output Picture", "The output was found at: " + output +
> ".");
> Log.Picture(pic_1, "Baseline Picture", "The baseline can be found at " +
> baseline + ".");
> Log.Picture(pic_1.Difference(pic_2, false, 5, false, 5, maskImg),
> "Comparison Result Image", "The baseline can be found at: " + baseline +
> ".");
> // Post an error message
> Log.Error("The PDFs are NOT equal.");
> delete pic_1;
> delete pic_2;
> break;
>   }
> // Post a message that the pages are equal
> Log.Picture(pic_2, "Output Picture", "The output was found at: " + output +
> ".");
> Log.Picture(pic_1, "Baseline Picture", "The baseline can be found at " +
> baseline + ".");
> Log.Picture(pic_1.Difference(pic_2, false, 5, false, 5, maskImg),
> "Comparison Result Image", "The baseline can be found at: " + baseline +
> ".");
> Log.Checkpoint("The PDF Pages are equal.", "The baseline can be found at: "
> + baseline + ".");
> }
>    }
>    catch(e)
>    {
> Log.Error(e);
> Log.Error("There was an error comparing the PDFs.");
>    }
> }
>
> function getTotalPages(pdfFile)
> {
>    var docObj =
> JavaClasses.org_apache_pdfbox_pdmodel.PDDocument.load_3(pdfFile);
>    totalPages = docObj.getNumberOfPages();
>    docObj.close();
>    return totalPages;
> }
>
> function convertPageToPicture(pdfFile, pageIndex)
> {
>    var pageObj, imgBuffer, imgFile, imgFormat, pictureObj, fileName;
>
>    fileName = "C:\\Users\\" + Sys.UserName +
> "\\AppData\\Local\\Temp\\tempImage.jpg";
>
>    // Get the desired page
>    pageObj = getPage(pdfFile, pageIndex);
>
>    try
>    {
>      imgBuffer = pageObj.convertToImage();
>    }
>    catch(e)
>    {
>      Log.Error(e);
>      Log.Error("Unable to convert the file " + pdfFile + " into an image.
> ABORT!");
>    }
>    if (pageObj.getResources() != null)
>    {
>      pageObj.getResources().Clear();
>    }
>
>    // Create a new file to save
>    imgFile = JavaClasses.java_io.File.newInstance(fileName);
>
>    // Get the image format from the name
>    imgFormat = aqString.SubString(fileName, aqString.GetLength(fileName)-3,
> 3);
>
>    // Save the image to the created file
>    try
>    {
>      JavaClasses.javax_imageio.ImageIO.write(imgBuffer, "jpg", imgFile);
>    }
>    catch(e)
>    {
>      Log.Error(e);
>      Log.Error("Unable to write the file to " + fileName + ".");
>    }
>
>    // Create a Picture object
>    pictureObj = Utils.Picture;
>
>    // Load the image as a picture
>    pictureObj.LoadFromFile(fileName);
>    aqFileSystem.DeleteFile(fileName);
>
>    imgBuffer.Flush();
>    return pictureObj;
> }
>
> ComparePDFs(pdf1.pdf, pdf2.pdf);
> =================================================
>


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: users-help@pdfbox.apache.org