Monday, March 24, 2014

Visual Studio Internal Error: 1058

Recently I was trying to install Visual Studio 2012 replacing the 2010 and I got the same error no matter what I tried.  The error which showing in the log read something like

- DDSet_Error: Internal error: 1058

It was a nasty one and I spent three nights trying to figure it out.  Eventually these are the things I tried and what actually solved my problem:

  1. Check any ensure that HTTP Activation is "on". You can do this by going to Control Panel->Add/Remove Programs->Turn WIndows features on or off->.NET Framework 3.5-> Windows Communication Foundation HTTP Activation
  2. Run Visual Studio 2010 Uninstall Utility in command prompt with switch VS2010_Uninstall-RTM.ENU.exe /full /netfx to clean the failed setup
  3. Clean the %temp% folder (Start Menu >> Run >> Type "%temp% >> OK)
  4. Started all Asp.Net , .NET related services 
  5. I installed XAMPP recently and had to disable HTTP under device manager (hidden devices/non-pnp) to get Apache to start on port 80 - This was what actually fixed the issue for me

Hope this helps 

Thursday, January 30, 2014

NEXTGEN Gallery Plugin for Arabic sites

Today I was trying out NextGen Gallery on an Arabic wordpress site.  Its an awesome plugin except that it doesn't have support for Arabic(UTF) album/gallery titles out of the box.  The way it works is whenever you create a gallery it uses the gallery title for three fields in the database, 'Name', 'Slug' & 'Title'.  It also creates a folder under /wp-content/gallery/ with the gallery name.  When creating an album the title entered is used to generate a Slug.  In both cases when using the utf encoded slug again - my links don't work.  If the folder gallery name is encoded in utf also , images won't even appear on the admin side.
It would have been nice if there was a slug field on the admin side.  After some digging and testing I noticed that if an album or gallery title is changed from the admin side, the album slug will not get the new value nor will the gallery folder name.  The gallery slug will be reset to the new title though.  The other gallery field that doesn't get updated is the 'Name'.
I figured a workaround for the problem would be to create the album / gallery using Latin characters and then change them to the Arabic title that I want display.   products\photocrati_nextgen\modules\ngglegacy\lib\ngg-db.phpTo have that work I need to make sure the slug of the gallery doesn't get updated or always takes the value of the name field.
Searching through NextGen code for "UPDATE $wpdb->nggallery" I found it in 4 files but only 3 had the slug reset:

\products\photocrati_nextgen\modules\ngglegacy\admin\ajax.php
\products\photocrati_nextgen\modules\ngglegacy\admin\manage.php
\products\photocrati_nextgen\modules\ngglegacy\lib\ngg-db.php

before the update statement you would see something like:
$slug = nggdb::get_unique_slug( sanitize_title( $title ), 'gallery' );

That needs to be changed to use the $name field or commented out if the name field is not available.


Note: The get_unique_slug function ensures uniqueness of the slug across all data types.
Warning:  This is only a workaround and might be overriden by new updates installed for the plugin.

Example:
becomes:





Thursday, January 16, 2014

PDF to TIFF in Java

Recently I came across some code to convert PDF to Tiff.  I restructured it into a nice clean utility class and provided some overloads for the main covert method.
Requires:

  • pdfbox-app-1.8.3.jar
  • jai_imageio-1.1.jar
  • log4j-1.2.16.jar
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.List;

import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageTypeSpecifier;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
import javax.imageio.metadata.IIOInvalidTreeException;
import javax.imageio.metadata.IIOMetadata;
import javax.imageio.stream.ImageOutputStream;

import org.apache.log4j.Logger;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;

import com.sun.media.imageio.plugins.tiff.BaselineTIFFTagSet;
import com.sun.media.imageio.plugins.tiff.TIFFDirectory;
import com.sun.media.imageio.plugins.tiff.TIFFField;
import com.sun.media.imageio.plugins.tiff.TIFFTag;
import com.sun.media.imageioimpl.plugins.tiff.TIFFImageWriterSpi;

public class PDFToTiff
{
   private static final Logger log = Logger.getLogger(PDFToTiff.class);
   
   public static void main(String[] args) throws IOException
   {
   
      try
      {
         //usage
         new PDFToTiff().convert("data/sample.pdf", null  , null );
         new PDFToTiff().convert("data/sample.pdf", "out/"    );
         new PDFToTiff().convert("data/sample.pdf", "out/" , "TestPrefix"   );
    
       
      }
      catch(Exception e)
      {
         // TODO Auto-generated catch block
         e.printStackTrace();
      }
   }
  
   /***
    * Converts pdfFile to Tiff images and places them in the same folder.
    * Tiff files will use same name of the input pdffile and a suffix of the page number
    * @param pdfFilePath
    * @throws Exception
    */
   public void convert(String pdfFilePath) throws Exception
   {
      convert(pdfFilePath,null,null);
   }
  
   /**
    * Converts pdfFile to Tiff images and places them in the output folder or the same folder if outFolder is null.
    * Tiff files will use same name of the input pdffile name and a suffix of the page number
    * @param pdfFilePath
    * @param outFolder
    * @throws Exception
    */
   public void convert(String pdfFilePath, String outFolder) throws Exception
   {
      convert(pdfFilePath,outFolder,null);
   }
   /***
    * Converts pdfFile to Tiff images and places them in the output folder or the same folder if outFolder is null.
    * Tiff file names will use outFilePrefix + page number or input pdffile name + page number if outFilePrefix is null
    * @param pdfFile
    * @param outFolder if null uses same location as pdfFile
    * @param outFilePrefix if null uses the name of the pdfFile
    * @throws Exception
    */
   public void convert(String pdfFilePath, String outFolder, String outFilePrefix) throws Exception
   {
      File pdfFile = new File(pdfFilePath);
      File outputFolder = outFolder==null? pdfFile.getParentFile() : new File (outFolder);
      if (!pdfFile.exists())
         throw new Exception("File {"+pdfFilePath+"} not found!");
   
      if (!outputFolder.exists())
      {
         log.debug("Creating output Folder " + outputFolder.getAbsolutePath());
         if (!outputFolder.mkdirs())
            throw new Exception("Could not created folder : " + outFolder);
      }
     
      if (outFilePrefix==null)
      {
         outFilePrefix = pdfFile.getName();
         //exclude the extension;
         if (outFilePrefix.endsWith(".pdf"))
            outFilePrefix = outFilePrefix.substring(0,outFilePrefix.lastIndexOf("."));
      }
      log.debug("Converting " + pdfFile.getName() + " to Tiff in " + outputFolder.getAbsolutePath() + " using prefix: " + outFilePrefix  );
     
      PDDocument document = null;
      int resolution = 300;
      try {
         // Set main options
         int imageType = BufferedImage.TYPE_BYTE_BINARY;
    
         int maxPage = Integer.MAX_VALUE;
     
         // Create TIFF writer
         ImageWriter writer = null;
         ImageWriteParam writerParams = null;
         try {
             TIFFImageWriterSpi tiffspi = new TIFFImageWriterSpi();
             writer = tiffspi.createWriterInstance();
             writerParams = writer.getDefaultWriteParam();
             writerParams.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
             writerParams.setCompressionType("CCITT T.6");
             writerParams.setCompressionQuality(1.0f);
         } catch (Exception ex) {
             throw new Exception("Could not load the TIFF writer", ex);
         }
     
         // Create metadata for Java IO ImageWriter
         IIOMetadata metadata = createMetadata(writer, writerParams, resolution);
     
         // Load the pdf file
         document = PDDocument.load(pdfFile);
         @SuppressWarnings("unchecked")
         List pages = (List) document.getDocumentCatalog().getAllPages();
         // Loop over the pages
         for (int i = 0; i < pages.size() && i <= maxPage; i++) {
             // Write page to image file
             File outputFile = null;
             ImageOutputStream ios = null;
             try {
                 outputFile = new File(outputFolder.getAbsolutePath()  + File.separator  +  outFilePrefix  + "_" + (i+1) + ".tiff");
                 ios = ImageIO.createImageOutputStream(outputFile);
                 writer.setOutput(ios);
                 writer.write(null, new IIOImage(pages.get(i).convertToImage(imageType, resolution), null, metadata), writerParams);
             } catch (Exception ex) {
                 throw new Exception("Could not write the TIFF file", ex);
             } finally {
                 ios.close();
             }
     
             // do something with TIF file
         }
         log.debug("Successfully converted " + pages.size() + " to Tiff.");
     } catch (Exception ex) {
         throw new Exception("Error while converting PDF to TIFF", ex);
     } finally {
         try {
             if (document != null) {
                 document.close();
             }
         } catch (Exception ex) {
             log.error("Error while closing PDF document", ex);
         }
     }
   }
  
  
   private IIOMetadata createMetadata(ImageWriter writer, ImageWriteParam writerParams, int resolution) throws IIOInvalidTreeException {
      // Get default metadata from writer
      ImageTypeSpecifier type = writerParams.getDestinationType();
      IIOMetadata meta = writer.getDefaultImageMetadata(type, writerParams);
  
      // Convert default metadata to TIFF metadata
      TIFFDirectory dir = TIFFDirectory.createFromMetadata(meta);
  
      // Get {X,Y} resolution tags
      BaselineTIFFTagSet base = BaselineTIFFTagSet.getInstance();
      TIFFTag tagXRes = base.getTag(BaselineTIFFTagSet.TAG_X_RESOLUTION);
      TIFFTag tagYRes = base.getTag(BaselineTIFFTagSet.TAG_Y_RESOLUTION);
  
      // Create {X,Y} resolution fields
      TIFFField fieldXRes = new TIFFField(tagXRes, TIFFTag.TIFF_RATIONAL, 1, new long[][] { { resolution, 1 } });
      TIFFField fieldYRes = new TIFFField(tagYRes, TIFFTag.TIFF_RATIONAL, 1, new long[][] { { resolution, 1 } });
  
      // Add {X,Y} resolution fields to TIFFDirectory
      dir.addTIFFField(fieldXRes);
      dir.addTIFFField(fieldYRes);
  
      // Return TIFF metadata so it can be picked up by the IIOImage
      return dir.getAsMetadata();
  }
  
  
}