techprimate/TPPDF

Image as background for every page

Closed this issue · 3 comments

Hello,

Is it possible to set an image as background in every page?
I see there is an open issue regarding changing the color of the page, but not setting an illustration.

Probably would be nice to have the option to set it like:
document.background.image = UIImage..

Hi!

You should be able to that by creating a group of elements and setting it on document.masterGroup

Closed due to inactivity

Hello @philprime! Thank you for your answer, I finally found the time to test your suggestion.
document.masterGroup seems working only with background color, but not with background images.

Here is the code I am trying:

` let pdfDocument = PDFDocument(format: .a4)
pdfDocument.background.color = .blue

    let testGroup = PDFGroup(allowsBreaks: true, backgroundColor: .systemPink)
    testGroup.add(textObject: PDFSimpleText(text: "Hello,"))
    testGroup.add(textObject: PDFSimpleText(text: "World!"))
    
    if let imageData = UserDefaults.standard.object(forKey: DefaultPreferencesManager.pageBackgroundImage) as? Data,
       let backgroundImage = UIImage(data: imageData)?.withRenderingMode(.alwaysOriginal) {
        let pdfImage = PDFImage(image: backgroundImage, quality: 1.0, options: .none)
        
        let masterGroup = PDFMasterGroup(isFullPage: true)
        masterGroup.backgroundImage = pdfImage
        masterGroup.backgroundColor = .systemGray
        pdfDocument.set(master: masterGroup)
        
        // Check if image is visible
        pdfImage.size = CGSize(width: 50, height: 60)
        testGroup.add(image: pdfImage)
    }
    
    pdfDocument.add(group: testGroup)
    pdfDocument.createNewPage()
    pdfDocument.add(group: testGroup)`

I added the same pdf image to a PDFGroup in order to show that the image is loaded correctly.
When I use masterGroup.backgroundColor = .systemGray the background color (gray) is correctly displayed in every page behind the other containers, as expected.
I attached 2 screenshots, one with the gray background color and the other with the pdf image in the PDFMasterGroup.
When I try only to display the background image from the PDFMasterGroup, it doesn't appear.

Is this a bug or am I missing something?

Simulator Screenshot - iPhone 8 iOS 16 4 - 2023-09-30 at 13 34 56
Simulator Screenshot - iPhone 8 iOS 16 4 - 2023-09-30 at 13 33 44