New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
PDF header #40
Comments
Not sure what you mean. Example code? |
Sorry, I'll try to explain: the following fails after first saveImage it seems, before it didn't have a problem with multiple runs.
|
Still can't reproduce... import drawBot
drawBot.size('A3Landscape')
for i in range(2):
drawBot.newDrawing()
for j in range(2):
drawBot.newPage(500, 500)
rect(10, 10, 200, 200)
drawBot.saveImage("PDF-issue-40-%s.pdf" % i) |
But something has changed with size() and newPage() indeed: you can't call size() after you've already drawn things, or call size() twice. So maybe that's it? |
Thanks, the example works. Is there a way to reset the context or clear drawBot after saving the PDF? Size needs to be set once before I run my export routines but needs to be different from time to time... |
newDrawing() should do that. (In the example the first size() does nothing, as newDrawing() resets everything.) I'm stil interested in your non-working example, as that PDF warning sounds suspicious. |
Right, so I probably switched newDrawing() and size(). Here's the relevant code I think:
|
If you could reduce that to a snippet we can just run, that would be great :) |
using its advised to use: newDrawing() # initiate a clear document
newPage(w, h) # add a page
# draw stuff to know width, height of predefined sizes before you draw or setup a document use: |
I'm setting size after newDrawing and the second error (warning) has disappeared, still get the first one however. Might be a mistake on my side, I'll do some testing. If I stil yet the header error I'll send you a snippet. Also I'll start using newPage(), thanks! |
The PDF header error seems to occur when loading an image; tested with both png and tif files. Here's the example: |
Ok, I see the warning now. It also happens when just running in DrawBot, but the msg gets sent to the low level stderr, so it gets hidden there. The warning is not written by DrawBot but by the OS. |
Ah, I have a suspicion what causes it: when placing and image, DrawBot tests to see whether it may be PDF data by attempting to instantiate a PDFDocument. If that fails, it's not PDF. I'm pretty sure that causes the warning. Frederik: should we revert to checking the file extension? |
Yes, it seems to be an Objective C error that happens when opening a PDF. The funny thing is the output is fine. |
the def isPDF(path):
if os.path.splitext(path)[-1].lower() != ".pdf":
return False
url = AppKit.NSURL.fileURLWithPath_(path)
return AppKit.PDFDocument.alloc().initWithURL_(url) is not None |
Or maybe (inspired by the actual issue here...): def isPDF(path):
return open(path).read(4) == "%PDF" |
yeah, what if the path is a URL? gr Frederik
|
NSData could be used def isPDF(path):
url = AppKit.NSURL.fileURLWithPath_(path)
data = AppKit.NSData.dataWithContentsOfURL_(url)
if data is None:
return False
if data.getBytes_range_(None, (0, 4)) != "%PDF":
return False
return AppKit.PDFDocument.alloc().initWithURL_(url) is not None |
Hm, but that's very wasteful if the file isn't a PDF, unless we can write it in a way that the data is only fetched once, regardless of image type. If that's hard: let's stick to file extensions. |
Getting a traceback when loading an image, seems to be related to this issue: Traceback (most recent call last): |
Strange I cannot find what I missed while adding the function are you sure you have to the latest version? and reinstalled is as module? |
Yes it's updated to the latest version. I'm softlinking to the module from site-packages, could that be the problem? |
Sorry, I meant linking through a pth file, not a softlink. |
oh, my bad, should return a tuple fixed e6fb858 should work in 3.77 thanks |
Great, drawing an image working again and the error is gone. Thanks for fixing! |
when importing DrawBot and generating a PDF, I'm getting an error after an update;
failed to find PDF header:
%PDF' not found.`as a warning, but second PDF won't build with this error:
It is advised to use 'size()' at the top of a script
Is this a new issue or am I doing something wrong?
The text was updated successfully, but these errors were encountered: