Site iconJava PDF Blog

PDF to HTML5 conversion – changing the page size

By default, a we convert a PDF file into an HTML file with the same size. So a 200×100 pixel PDF file becomes a 200×100 pixel HTML file. You can easily scale in/out on most mobile devices but sometimes it is just too big or small as a sensible default.

So we have added a scaling setting into the latest release (so that you can have 400×200 pixels or 50×25 pixels). Note that the scaling has to be the same on both sized (if you turn a 200×100 pixel image into a 150x150pixel image, it will becomes ‘squashed’. It is important to maintain the aspect ratio and treat both sides the same.

Sometimes, however, we might want to scale the image up so that it makes the best fit into a different sized hole in our case, we would need to scale the image to 150x75pixels. This is very easy to do. All we need to do is calculate the scaling needed for height and width to fit the maximum gap and choose the smaller of the two values. Here is the sample code (which is also in our updated example). Happy scaling!

 

DynamicVectorRenderer HTMLoutput=new HTMLDisplay(page, midPoint, 
cropBox ,false, 100, new ObjectStore());

//have a scaling factor so we can alter the page size
float scaling=1.0f;

/**
 * if you want to fit it to a certain size, use this code
 * work out max possible scaling for both width and height
 * and use smaller to get max possible size but retain aspect ratio
 * - will not always be exact match as preserves aspect ratio
**/float preferredWidth=1000,preferredHeight=1000;

// scaling we need to scale w up to our value
float scalingX=preferredWidth/cropW; 
// scaling we need to scale w up to our value
float scalingY=preferredHeight/cropH; 

if(scalingX>scalingY)
   scaling=scalingY;
else
   scaling=scalingX;

//set page scaling (default is 100%)
HTMLoutput.setValue(HTMLDisplay.PercentageScaling, (int) (scaling*100));

Click here to see all the articles in the PDF to HTML5 conversion series.