background

Convert a GIF shipping label image to PDF

Published on 31 January 2017 in apex salesforce 

In today’s blog post we’re going to show you the codes needed to convert a GIF shipping label into a PDF document.

We’re going to use pages, classes, and controllers.

APEX Class

The first method we need to look at is regenerateGifToPdfLabel (line 2). The steps are as follows:

The createPDFLabels (line 25) method in step 3 above calls the generatePDFLabel method (line 35).

Next create a new attachment object pdfLabel, which will be the PDF file (line 36). Line 38 is where we reference our VisualForce page, that will contain the original GIF file. The VisualForce page code can be found at the bottom of this post.

Line 41 sets the Body of the new attachment object as the blob that is returned from the standard SalesForce method getContentAsPDF();.

public with sharing class UPS_LabelGifToPDFHandler {
public static void regenerateGifToPdfLabel(Set<String> shipmentsIds){
List<Attachment> allLabels = [
SELECT Name, ParentId, ContentType
FROM Attachment
WHERE ParentId IN :shipmentsIds
LIMIT 99
];
List<Attachment> gifLabels = new List<Attachment>();
for (Attachment record : allLabels ){
if ( isNeedGeneratePDFLabel(record) ){
gifLabels.add(record);
}
}
if ( ! gifLabels.isEmpty() ){
List<Attachment> pdfLabels = createPDFLabels(gifLabels);
insert pdfLabels;
delete gifLabels;
}
}
private static List<Attachment> createPDFLabels(List<Attachment> gifLabels){
List<Attachment> pdfLabels = new List<Attachment>();
for ( Attachment gifLabel : gifLabels ) {
pdfLabels.add(generatePDFLabel(gifLabel));
}
return pdfLabels;
}
private static Attachment generatePDFLabel(Attachment gifLabel){
Attachment pdfLabel = new Attachment();
PageReference pdfPageGenerator = Page.ShipmentLabelPDF;
pdfPageGenerator.getParameters().put('id', gifLabel.Id);
pdfLabel.Body = pdfPageGenerator.getContentAsPDF();
pdfLabel.Name = replaceExtensiontoPdf(gifLabel.Name);
pdfLabel.ContentType = 'application/pdf';
pdfLabel.ParentId = gifLabel.ParentId;
return pdfLabel;
}
private static String replaceExtensiontoPdf(String fileName){
return (fileName.substring(0, fileName.length() - 4) + '.PDF');
}
private static Boolean isNeedGeneratePDFLabel(Attachment attachment){
return ( isContentTypeGif(attachment.ContentType)
&& isParentObjectUPSShipment(attachment.ParentId) );
}
private static Boolean isParentObjectUPSShipment(Id parentId){
Schema.SObjectType sobjectType = parentId.getSObjectType();
return (sobjectType.getDescribe().getName() == 'zkups__UPSShipment__c');
}
private static Boolean isContentTypeGif(String format){
return (format == 'GIF');
}
}

APEX Controller

Line 6-7 basically set the variable that will be used in the VisualForce page.

public with sharing class ShipmentLabelPDF {
public String gifLabelURL {get; set;}
private Id gifLabelId;
public ShipmentLabelPDF() {
this.gifLabelId = ApexPages.currentPage().getParameters().get('id');
this.gifLabelURL = '/servlet/servlet.FileDownload?file=' + gifLabelId;
}
}

VisualForce Page

Line 25 is where the GIF shipping label is inserted, which is then rendered as a pdf from the code we have already looked at from above.

<apex:page controller="ShipmentLabelPDF" applyHtmlTag="false" renderAs="pdf" applyBodyTag="false" showHeader="false" standardStylesheets="false" docType="html-5.0">
<html>
<head>
<style >
body {
display: inline;
margin-top: 0px;
margin-right: 0px;
margin-bottom: 0px;
margin-left: 0px;
}
@page{
size: 8.5in 11in;
margin-top: 0.5in;
margin-left: 0.5in;
margin-right: 0cm;
margin-bottom: 0cm;
padding: 0cm;
}
</style>
</head>
<body>
<img src="{! GifLabelURL }" style="height: 453px"></img>
</body>
</html>
</apex:page>

Happy coding.