omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    Welcome!

    This is the community forum for my apps Pythonista and Editorial.

    For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.


    for those interested, convert a pdf page into an ui.Image

    Pythonista
    1
    2
    1296
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • cvp
      cvp last edited by

      I use it for pdf covers as buttons images.

      from   objc_util import *
      import ui
      
      def GetUIImageOfPDFPage(path,pageid=1):
      	UIGraphicsGetCurrentContext = c.UIGraphicsGetCurrentContext
      	UIGraphicsGetCurrentContext.restype = c_void_p
      	UIGraphicsGetCurrentContext.argtypes = []
      
      	CGPDFDocumentCreateWithURL = c.CGPDFDocumentCreateWithURL
      	CGPDFDocumentCreateWithURL.restype = c_void_p
      	CGPDFDocumentCreateWithURL.argtypes = [c_void_p]
      
      	CGPDFDocumentGetNumberOfPages = c.CGPDFDocumentGetNumberOfPages
      	CGPDFDocumentGetNumberOfPages.restype = c_void_p
      	CGPDFDocumentGetNumberOfPages.argtypes = [c_void_p]
      
      	CGPDFDocumentGetPage = c.CGPDFDocumentGetPage
      	CGPDFDocumentGetPage.restype = c_void_p
      	CGPDFDocumentGetPage.argtypes = [c_void_p, c_void_p]
      
      	CGPDFPageGetBoxRect= c.CGPDFPageGetBoxRect
      	CGPDFPageGetBoxRect.restype = CGRect
      	CGPDFPageGetBoxRect.argtypes = [c_void_p,c_void_p]
      
      	CGContextDrawPDFPage = c.CGContextDrawPDFPage
      	CGContextDrawPDFPage.restype = None
      	CGContextDrawPDFPage.argtypes = [c_void_p,c_void_p]
      
      	ns_url = nsurl(path)
      	document = CGPDFDocumentCreateWithURL(ObjCInstance(ns_url))
      	#np = CGPDFDocumentGetNumberOfPages(document)
      	#print('CGPDFDocumentGetNumberOfPages=',np)	# to check if document is ok
      
      	page = CGPDFDocumentGetPage(document,pageid)
      
      	page_mediabox = CGPDFPageGetBoxRect(page,0)	# CGPDFBox=0=>kCGPDFMediaBox
      	w = page_mediabox.size.width
      	h = page_mediabox.size.height
      	#print('mediabox=',w,h)			# to check if mediabox is ok
      
      	with ui.ImageContext(w,h) as context:
      		cn = UIGraphicsGetCurrentContext()
      		CGContextDrawPDFPage(cn,page)
      		ui_image = context.get_image()
      	return ui_image
      
      # Protect against import	
      if __name__ == '__main__':
      	full_path = '......pdf'
      	ui_image = GetUIImageOfPDFPage(full_path,pageid=40)
      	v = ui.ImageView()
      	v.image = ui_image
      	v.transform = ui.Transform.scale(1,-1)	# CGContext is up/down inverted
      	v.present(hide_title_bar=True) 
      
      1 Reply Last reply Reply Quote 1
      • cvp
        cvp last edited by

        We can do the transform inside the def via ObjectiveC

        from   objc_util import *
        import ui
        
        def GetUIImageOfPDFPage(path,pageid=1):
        	UIGraphicsGetCurrentContext = c.UIGraphicsGetCurrentContext
        	UIGraphicsGetCurrentContext.restype = c_void_p
        	UIGraphicsGetCurrentContext.argtypes = []
        
        	CGPDFDocumentCreateWithURL = c.CGPDFDocumentCreateWithURL
        	CGPDFDocumentCreateWithURL.restype = c_void_p
        	CGPDFDocumentCreateWithURL.argtypes = [c_void_p]
        
        	CGPDFDocumentGetNumberOfPages = c.CGPDFDocumentGetNumberOfPages
        	CGPDFDocumentGetNumberOfPages.restype = c_void_p
        	CGPDFDocumentGetNumberOfPages.argtypes = [c_void_p]
        
        	CGPDFDocumentGetPage = c.CGPDFDocumentGetPage
        	CGPDFDocumentGetPage.restype = c_void_p
        	CGPDFDocumentGetPage.argtypes = [c_void_p, c_void_p]
        
        	CGPDFPageGetBoxRect= c.CGPDFPageGetBoxRect
        	CGPDFPageGetBoxRect.restype = CGRect
        	CGPDFPageGetBoxRect.argtypes = [c_void_p,c_void_p]
        
        	CGContextDrawPDFPage = c.CGContextDrawPDFPage
        	CGContextDrawPDFPage.restype = None
        	CGContextDrawPDFPage.argtypes = [c_void_p,c_void_p]
        	
        	CGContextScaleCTM = c.CGContextScaleCTM
        	CGContextScaleCTM.restype =  None
        	CGContextScaleCTM.argtypes = [c_void_p, CGFloat, CGFloat]
        	
        	CGContextTranslateCTM = c.CGContextTranslateCTM
        	CGContextTranslateCTM.restype = None
        	CGContextTranslateCTM.argtypes = [c_void_p, CGFloat, CGFloat]
        
        	ns_url = nsurl(path)
        	document = CGPDFDocumentCreateWithURL(ObjCInstance(ns_url))
        	#np = CGPDFDocumentGetNumberOfPages(document)
        	#print('CGPDFDocumentGetNumberOfPages=',np)	# to check if document is ok
        
        	page = CGPDFDocumentGetPage(document,pageid)
        
        	kCGPDFMediaBox = 0
        	page_mediabox = CGPDFPageGetBoxRect(page,kCGPDFMediaBox)
        	w = page_mediabox.size.width
        	h = page_mediabox.size.height
        	#print('mediabox=',w,h)			# to check if mediabox is ok
        
        	with ui.ImageContext(w,h) as context:
        		cn = UIGraphicsGetCurrentContext()
        		CGContextTranslateCTM(cn,0.0,h)
        		CGContextScaleCTM(cn, 1.0, -1.0)
        		CGContextDrawPDFPage(cn,page)
        	return ui_image
        	
        # Protect against import	
        if __name__ == '__main__':
        	full_path = '/private/var/mobile/Library/Mobile Documents/iCloud~com~omz-software~Pythonista3/Documents/Mes Livres/Les Blondes - La compil des vacances.pdf'
        	ui_image = GetUIImageOfPDFPage(full_path,pageid=40)
        	v = ui.ImageView()
        	v.image = ui_image
        	#v.transform = ui.Transform.scale(1,-1)	# CGContext is up/down inverted
        	v.present(hide_title_bar=True) 
        
        1 Reply Last reply Reply Quote 0
        • First post
          Last post
        Powered by NodeBB Forums | Contributors