ios html转pdf,iphone - Convert html file to PDF Document in iOS using Cocoa-Touch - Stack Overflow

I want to convert local html file on iPhone to PDF document. I have used the following code I found online to generate PDF file from Picture, I tried to replace the image with the html file but it failed. I would very much appreciate your help.

Thanks in advance,

// Our method to create a PDF file natively on the iPhone

// This method takes two parameters, a CGRect for size and

// a const char, which will be the name of our pdf file

void CreatePDFFile (CGRect pageRect, const char *filename) {

// This code block sets up our PDF Context so that we can draw to it

CGContextRef pdfContext;

CFStringRef path;

CFURLRef url;

CFMutableDictionaryRef myDictionary = NULL;

// Create a CFString from the filename we provide to this method when we call it

path = CFStringCreateWithCString (NULL, filename,

kCFStringEncodingUTF8);

// Create a CFURL using the CFString we just defined

url = CFURLCreateWithFileSystemPath (NULL, path,

kCFURLPOSIXPathStyle, 0);

CFRelease (path);

// This dictionary contains extra options mostly for 'signing' the PDF

myDictionary = CFDictionaryCreateMutable(NULL, 0,

&kCFTypeDictionaryKeyCallBacks,

&kCFTypeDictionaryValueCallBacks);

CFDictionarySetValue(myDictionary, kCGPDFContextTitle, CFSTR("My PDF File"));

CFDictionarySetValue(myDictionary, kCGPDFContextCreator, CFSTR("My Name"));

// Create our PDF Context with the CFURL, the CGRect we provide, and the above defined dictionary

pdfContext = CGPDFContextCreateWithURL (url, &pageRect, myDictionary);

// Cleanup our mess

CFRelease(myDictionary);

CFRelease(url);

// Done creating our PDF Context, now it's time to draw to it

// Starts our first page

CGContextBeginPage (pdfContext, &pageRect);

// Draws a black rectangle around the page inset by 50 on all sides

CGContextStrokeRect(pdfContext, CGRectMake(50, 50, pageRect.size.width - 100, pageRect.size.height - 100));

// This code block will create an image that we then draw to the page

const char *picture = "Picture";

CGImageRef image;

CGDataProviderRef provider;

CFStringRef picturePath;

CFURLRef pictureURL;

picturePath = CFStringCreateWithCString (NULL, picture,

kCFStringEncodingUTF8);

pictureURL = CFBundleCopyResourceURL(CFBundleGetMainBundle(), picturePath, CFSTR("png"), NULL);

CFRelease(picturePath);

provider = CGDataProviderCreateWithURL (pictureURL);

CFRelease (pictureURL);

image = CGImageCreateWithPNGDataProvider (provider, NULL, true, kCGRenderingIntentDefault);

CGDataProviderRelease (provider);

CGContextDrawImage (pdfContext, CGRectMake(200, 200, 207, 385),image);

CGImageRelease (image);

// End image code

// Adding some text on top of the image we just added

CGContextSelectFont (pdfContext, "Helvetica", 16, kCGEncodingMacRoman);

CGContextSetTextDrawingMode (pdfContext, kCGTextFill);

CGContextSetRGBFillColor (pdfContext, 0, 0, 0, 1);

const char *text = "Hello World!";

CGContextShowTextAtPoint (pdfContext, 260, 390, text, strlen(text));

// End text

// We are done drawing to this page, let's end it

// We could add as many pages as we wanted using CGContextBeginPage/CGContextEndPage

CGContextEndPage (pdfContext);

// We are done with our context now, so we release it

CGContextRelease (pdfContext);

}

- (IBAction)createPDF:(id)sender {

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

NSString *saveDirectory = [paths objectAtIndex:0];

NSString *saveFileName = @"myPDF.pdf";

NSString *newFilePath = [saveDirectory stringByAppendingPathComponent:saveFileName];

const char *filename = [newFilePath UTF8String];

CreatePDFFile(CGRectMake(0, 0, 612, 792),filename);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值