osx - Apply a Quartz filter while saving PDF under Mac OS X 10.6.3 -


using mac os x api, i'm trying save pdf file quartz filter applied, possible "save as" dialog in preview application. far i've written following code (using python , pyobjc, isn't important me):

-- filter-pdf.py: begin

from foundation import * quartz import * import objc  page_rect = cgrectmake (0, 0, 612, 792) fdict = nsdictionary.dictionarywithcontentsoffile_("/system/library/filters/blue \ tone.qfilter") in_pdf = cgpdfdocumentcreatewithprovider(cgdataprovidercreatewithfilename ("test .pdf")) url = cfurlcreatewithfilesystempath(none, "test_out.pdf", kcfurlposixpathstyle,  false) c = cgpdfcontextcreatewithurl(url, page_rect, fdict)  np = cgpdfdocumentgetnumberofpages(in_pdf) ip in range (1, np+1):         page = cgpdfdocumentgetpage(in_pdf, ip)         r = cgpdfpagegetboxrect(page, kcgpdfmediabox)         cgcontextbeginpage(c, r)         cgcontextdrawpdfpage(c, page)         cgcontextendpage(c) 

-- filter-pdf.py: end

unfortunalte, filter "blue tone" isn't applied, output pdf looks input pdf.

question: missed? how apply filter?

well, documentation doesn't promise such way of creating , using "fdict" should cause filter applied. rewritten (as far can) sample code /developer/examples/quartz/python/filter-pdf.py, distributed older versions of mac (meanwhile, code doesn't work too):

----- filter-pdf-old.py: begin

from coregraphics import * import sys, os, math, getopt, string  def usage ():   print ''' usage: python filter-pdf.py filter input-pdf output-pdf  apply colorsync filter pdf document. '''  def main ():    page_rect = cgrectmake (0, 0, 612, 792)    try:     opts,args = getopt.getopt (sys.argv[1:], '', [])   except getopt.getopterror:     usage ()     sys.exit (1)    if len (args) != 3:     usage ()     sys.exit (1)    filter = cgcontextfiltercreatedictionary (args[0])   if not filter:     print 'unable create context filter'     sys.exit (1)   pdf = cgpdfdocumentcreatewithprovider (cgdataprovidercreatewithfilename (args[1]))   if not pdf:     print 'unable open input file'     sys.exit (1)    c = cgpdfcontextcreatewithfilename (args[2], page_rect, filter)   if not c:     print 'unable create output context'     sys.exit (1)    p in range (1, pdf.getnumberofpages () + 1):     #r = pdf.getmediabox (p)     r = pdf.getpage(p).getboxrect(p)     c.beginpage (r)     c.drawpdfdocument (r, pdf, p)     c.endpage ()    c.finish ()  if __name__ == '__main__':   main () 

----- filter-pdf-old.py: end

=======================================================================

the working code based on answer:

from foundation import * quartz import *  pdf_url = nsurl.fileurlwithpath_("test.pdf") pdf_doc = pdfdocument.alloc().initwithurl_(pdf_url)  furl = nsurl.fileurlwithpath_("/system/library/filters/blue tone.qfilter") fobj = quartzfilter.quartzfilterwithurl_(furl) fdict = { 'quartzfilter': fobj } pdf_doc.writetofile_withoptions_("test_out.pdf", fdict) 

two approaches - if need open , modify existing file, use pdfkit's pdfdocument (reference) , use pdfdocument's writetofile_withoptions_ option dict including "quartzfilter" option of needed filter.

otoh if need own drawing , have cgcontext @ hand, can use along these lines:

from quartz import * data = nsmutabledata.datawithcapacity_(1024**2) dataconsumer = cgdataconsumercreatewithcfdata(data) context = cgpdfcontextcreate(dataconsumer, none, none) f = quartzfilter.quartzfilterwithurl_(nsurl.fileurlwithpath_("yourfltr.qfilter")) f.applytocontext_(context) # drawing cgpdfcontextclose(context) # pdf in data variable. whatever need data (save file...). 

Comments

Popular posts from this blog

javascript - Enclosure Memory Copies -

php - Replacing tags in braces, even nested tags, with regex -