How to embed font into PDF file displayed in UIWebview? - iphone

I am opening a pdf file downloaded from server into UIWebview. However, the pdf opens correctly into the web view, but there is an error displayed on console:
LLALCK+MyriadPro-SemiboldIt: FT_Select_Charmap failed: error 6
After some r&d I found that this issue is related to fonts.
I would like to know how to embed fonts within the pdf, so that the above error is eliminated. I tried installing font.otf file on Mac and tried to run pdf display on simulator, but it still gives same error.
Following is the code which I use to display pdf in uiwebview:
NSData* data = [NSData dataWithContentsOfFile:path];
[webView
loadData:data
MIMEType:#"application/pdf"
textEncodingName:#"UTF-8"
baseURL:nil];
Please help.
Badly stuck!
Thanks in advance.

The problem is your PDF references a font which the device does not have. You could try adding this font to your bundle, but this can be problematic in several key areas.First, I am not sure WebView can read custom fonts for the purpose of PDFs; but more importantly, second, you likely do not have a license to embed that font in your application.
I would suggest tackling this problem from a different perspective. How is your PDF created on the server? Either use a safe font in your PDF (see a full list of iOS fonts here), or use the font embedding feature of PDF and use that. Fonts usually come with a more relaxed license when embedding certain glyph in PDFs.

Related

Rendering Images inside PDF, images not being rendered in PDF Viewer 0.1.8

Please check updates as they have additional informations... Apparently located the problem in a specific pdf client but cannot close the issue with an open bounty...
I am generating a pdf using grails rendering plugin. The PDF has a couple of images inside and "some" of them are not being outputted!
I am rendering the images inline via data uris as required by the plugin. That means that all my images are something like:
<img src="data:image/jpeg;base64,/9j/4AAQSkZJRgABAQECWAJYAAD...">
If I render them in a normal html view, I can see the images just fine!
If I render the template to a JPG/PNG with the same plugin, again the images render all fine.
If I render to PDF the images which are being retrieved by an octed-stream are broken!
Something like:
Looks like the image started to render and then something happened...
It is happening on the big-sized images, but also on the thumbnail version of same image.
Any one has some hints as why this might occur?
UPDATE
The file which does not show up is a file with mime application/octet-stream
So apparently I can retrieve the bytes from the file, but when they transmitted for PDF Rendering, the image does not appear...
Yet another update
The issue seems to be related with the PDF Viewer. Was using a Linux based PDF Viewer (PDF Viewer 0.1.8) and specific images are broken. In all other PDF Viewers I could test everything works fine.
Cannot close the issue as there is a bounty open :( Sorry that the bounty and question seems meaningless now, but you never know, someone might have an idea how to solve this even for PDF Viewer 0.1.8.
<img src="data:image/jpg;base64,/9j/4AAQSkZJRgABAQECWAJYAAD...">
works fine for me. Note the missing "e".
You can use rendering tag:
<rendering:inlineJpeg bytes="${your-image}" />
Make sure you decodeBase64() your image.

iphone epub reader how to change the font size/color programmatically?

I have seen many projects that reads epub files than parsing it to XML formats.
Some projects uses HTML to display the formatted ftext in a web view
Is there any way possible way to change the color/size of a font?
I think some are including Java Script files but still not sure...
Any framework that rcan help for epub content reading and customiziing....
ePub uses XHTML & CSS for content, so most (if not all) ePub readers will use a UIWebView.
Changing the font color & size, then, is simply a matter of fiddling with said UIWebView's stylesheets, whether through JavaScript or altering the XHTML/CSS files directly.

Programmatically Generate PDF from HTML on iPhone

I am looking for a way to programmatically (in obj-c) generate a PDF file from a local html file. I am dynamically generating the html from user inputs, I need to create the PDF and send it to the user (via email). I am having difficulty with the PDF generation portion.
I have the code to create a PDF using CGPDFContextCreateWithURL but I am struggling with drawing the page using quartz.
I have searched extensively on SO as well as the internet to no avail.
Any help is much appreciated!
To generate a pdf from an HTML, you need to render the html into a web view, and take snapshots of the web view, and render them into an image context.
The tutorial might be helpful:
http://www.ioslearner.com/convert-html-uiwebview-pdf-iphone-ipad/
I've written a little piece of code that takes an NSAttributedString from DTCoreText, and renders it into a paged PDF file. You can find it on my GitHub Repository. It won't render images or complex html, but it should serve for most uses. Plus, if you're familiar with CoreText, you can extend my PDF frame setter to generate these items.
So what it does now: Give it an HTML string, and it will use DTCoreText to generate an NSAttributedString, then render that into a PDF. It hands back the location that it saved the PDF file in the app's Documents folder.
Why not use a WebService, send the HTML page to this and retrieve the PDF-file ?
That way you can use iTextSharp and C#, and you're done in about 2 minutes.
Plus (if you're evil) you can store and see all the data on your server.
I haven't tried this myself so i have nothing to offer concrete but I'd have to imagine there has to be an easy way to do this on iPhone due to the imaging model. I'd look deeper into the documentation.
As to pushing back with the client that is up to you but there are probably multiple reasons for wanting to keep everything local. Frankly I would not be pleased at all to here from somebody I hired that he couldn't manage this particular task. So think long and hard about this push back. Oh even if you do push back a webserver is a poor choice. I'd go back a step further and investgate why you need something in HTML in the first place.
I've never tried this so I have no idea if it'll work, but how about loading the HTML into a UIWebView, and then make the view draw itself into a PDF context? E.g.
UIWebView *webview = [[UIWebView alloc] initWithFrame:CGRectMake(...)];
[webview loadHTMLString:html baseURL:...];
Then:
- (void)webViewDidFinishLoad:(UIWebView *)webview {
CGPDFContextRef pdfContext = CGPDFContextCreateWithURL(...);
[webview.layer drawInContext:pdfContext];
...
}
I made it by following this SO: https://stackoverflow.com/a/13342906/448717
In order to maintain the same content's proportions I had to multiply the size of the WKWebView 1.25 times the printableRect's size set for the UIPrinterRenderer, as the screen points differs from the PostScript's... I guess.

How to get asian fonts working on an iPad?

I have a PDF file with a font reference to STSong-Light which is not available on the iOS.
Therefor I can not display the correct characters defined with that font.
I took the STSong font file mentioned on Apples KB:
http://support.apple.com/kb/ht1538
… and put it into my project and added it to the plist.
The text is now no longer being rendered.
I'm using CGContextDrawPDFPage to render my PDF files.
My question is:
How can I correctly get my font file working OR can I somehow access on the iOS available fonts to replace STSong fonts in my PDF?
(there must be other asian fonts, otherwise these interfaces wouldn't work)
Thank you all in advance for your help!
Edit: I got some Feedback from Apple that I could also use a font-substitute which means the second part of the question should be the right direction.
Added: This is the source which opens the PDF Files:
CFURLRef pdfURL = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, (CFStringRef)pdfFilename, kCFURLPOSIXPathStyle, false);
CGPDFDocumentRef pdfRef = CGPDFDocumentCreateWithURL((CFURLRef)pdfURL);
CFRelease(pdfURL);
CGPDFPageRef pdfPage = CGPDFDocumentGetPage(pdfRef, page);
[..]
The font may not be as important as the encoding. If you are setting the encoding value for the document, try using 0x80000421. I have an application that presents Kanji text using the Helvetica font but with a string encoding of 0x80000421
Try This:
Instead of using CGURLCreateWithFileSystemPath(), try loading the file contents into an NSData object and use CFURLCreateWithBytes() using that NSData contents and use 0x80000421 as the value for CGStringEncodingRef

how to use custom font in html pages for UIWebView?

I am having the "Futura.ttf" font file.
I am displaying a HTML page in the UIWebView, but my requirement is that i want to use the custom font in my css file.
so is there any way that i can use the custom font in my css file ???
All suggestions are welcomed.
Thanks.
It is possible to load custom fonts into your UIWebView in iOS3.2 and above. Add the font to your bundle (see here) then just reference the font in your UIWebView's stylesheet like you would any other font:
<style type='text/css'>font { font-family: DroidSerif; } </style>
You have Cufon and sIFR as your options.
Typeface.js is a pure JavaScript Replacement
Cufon is a pure JavaScript Replacement
sIFR is Flash and Java font implementation,
FLIR JavaScript and PHP implementation
Some Comparisons
http://thatguynamedandy.com/blog/text-replacement-comparison
http://thinkclay.com/technology/cufon-sifr-flir
http://aaronwinborn.com/blogs/aaron/cufón-alternative-sifr-image-replacement
Below is taken from this question Worth reading the whole thread, has greatdetails.
Typeface.js
Advantages:
User doesn’t have to have Flash
plugin installed on their browser
Easier to create with just a few
lines of Javascript
For page loading it just needs to
load the Javascript
Disadvantages:
Text is not selectable because it
outputs it like an image. I looked at
some examples, right clicked on a
word and had to view as an image.
Every single word had this behaviour.
Big thumbs down.
Usage for body copy will slow down
loading time, so it is recommended to
use only for headlines.
Cannot be read by screen readers
Visual looks blurry
Not all browser compliant and still
has a lot of development left to be
done
sIFR
Advantages:
Can be read by screen readers as a
normal headline because it is a
behaviour layer on top of the markup
and styling.
Text is selectable
SEO friendly
Displays text as is like any other
web font. Crisp and not blurry!
Has addons like jQuery sIFR Plugin!
Disadvantages:
Requires Javascript to be enabled
Flash plugin must be installed in the
browser
Need Adobe Flash Studio to create it
BUT there is a pretty nifty sIFR
generator that creates the file for
you!
For page loading, it has to request
for Flash, Javascript and CSS files
attached to it, which can potentially
get bogged down if you are using sIFR
in too many places.
Cannot display on an iPhone. Yet…
Cufón (similar to Typeface.js)
Enter Cufón, the Javascript-based font replacement solution which makes heavy use of canvas and VML. This offers a great alternative to other solutions out there - no Flash or images required.
There are some issues with using Cufón on a live site, the most notable being the inability to highlight and copy/paste text, which is really the biggest issue for your site's users.
Combine that with the EULA issues, which prevent you from being able to legally embed fonts in Javascript files for most fonts on the market today.
The other issue is knowing what fonts can be used with Cufón. For sIFR, most fonts are fair game, since the font is embedded in a Flash movie, which is typically an approved usage by most font foundries for most fonts. With Cufón, the Javascript files used for the font can be easily "stolen" and either used on another website or reverse engineered.