Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conversion of UIView to PDF changes image colour

I have a repeatable pattern within my app.

Here's the image as displayed within the app:

Original Image

Here's the same image as displayed after conversion to PDF:

PDF Image - Pink bars instead of faded blue/grey

As you can see by this, the image colour changes from a grey/green to a pink.
The PDF is loaded directly into an email ready for sending.

I'm not sure if this is something to do with the image itself being loaded into the PDF, or something related to the PDFing process. Either way, I've attached the code of the PDF process below.

Here's my PDF generation code:

- (NSData *)createPDFForView:(UIView *)view{        
    NSMutableData *pdfData = [NSMutableData data];
    UIGraphicsBeginPDFContextToData(pdfData, CGRectMake(0, 0, 792, 600), nil);
    UIGraphicsBeginPDFPage();
    CGContextRef pdfContext = UIGraphicsGetCurrentContext();
    [view setFrame:CGRectMake(0, 0, 792, 600)];
    [view.layer renderInContext:pdfContext];
    UIGraphicsEndPDFContext();
    return pdfData;
}

During the PDF process, I convert it to 50% of the size that it is by adjusting the frame, the image itself is not modified, just the view that contains the image; the image autoresizes within the frame.

Is there anything that can be done to stop this colour change from occurring?

like image 966
Dan Hanly Avatar asked Jan 30 '26 01:01

Dan Hanly


2 Answers

PDF specification only supports JPEG, JPEG2000, TIFF & JBIG2 (and some other useless ones no one cares about.) Long story short is that whatever program you use to convert your document to PDF will be forced to convert the image to a compatible format, and JPEG is traditionally opted for in these cases.

So the reason is because the image was modified to JPEG without transparency, and using a PNG to JPEG converter that had no respect for retaining the appearance of your image. If the image had any color profiles these were likely stripped as well.

You will have considerably better results and more control over what goes on if you convert your images to JPEG before converting the document to PDF. This way the program will keep your images as they are, so you know exactly what you will get. This is also your only chance of specifying the color profile of the image.

If you can manage it, JPEG2000 is preferable over JPEG, especially if you're looking for lossless images.

Transparency can be achieved on a PDF but this is achieved with filters over the image object, irrespective of the image format used. However, that is a bit more advanced than what you are doing and probably not worth the time.

like image 142
Alasdair Avatar answered Feb 01 '26 18:02

Alasdair


I've solved the error, but not figured out the 'why' of it.

Essentially, the grey/blue colour was a transparent PNG.
I gave this a white background and re-saved the image and it seems to work fine.

I'll still award the bounty to anyone who can help me with the 'why'.

like image 20
Dan Hanly Avatar answered Feb 01 '26 16:02

Dan Hanly



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!