简体   繁体   中英

Convert DOC to one page PDF iOS

Using iOS-htmltopdf , i am able to convert DOC to PDF but i want single page PDF from doc file.

The reason is some content of one page gets into another page so only one page.

In NDHTMLtoPDF.m i use these 2 line of code then PDF page size becomes double

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
  if (webView.isLoading) return;

  long pageHeight = [[webView stringByEvaluatingJavaScriptFromString:@"document.body.scrollHeight;"]longLongValue];
  self.pageSize = CGSizeMake(595, webView.scrollView.contentSize.height/2);

  .........
  .........
  .........
}

Firstly convert DOC to PDF using NDHTMLtoPDF .

Below delegate method will be called

- (void)HTMLtoPDFDidSucceed:(NDHTMLtoPDF*)htmlToPDF
{
  NSLog(@"HTMLtoPDF did succeed (%@ / %@)", htmlToPDF, htmlToPDF.PDFpath);

  NSURL *pptURL = [NSURL fileURLWithPath:htmlToPDF.PDFpath];
  NSURLRequest *request = [NSURLRequest requestWithURL:pptURL];
  [myWebView loadRequest:request];

  //now merge whole pages to one PDF
  [self MergeToOnePagePDF:pptURL];   
}

Now add these method:

-(void)MergeToOnePagePDF:(NSURL *)pdfURL
{
  CGPDFDocumentRef pdf = CGPDFDocumentCreateWithURL((__bridge CFURLRef)pdfURL);

  int pageCount = CGPDFDocumentGetNumberOfPages(pdf);
  CGPDFPageRef pageRef = CGPDFDocumentGetPage(pdf, 1);
  CGRect pageRect = CGPDFPageGetBoxRect(pageRef, kCGPDFMediaBox);
  float pageHeight = pageRect.size.height;
  pageRect.size.height = pageRect.size.height * pageCount;

  NSMutableData* pdfData = [[NSMutableData alloc] init];
  CGDataConsumerRef pdfConsumer = CGDataConsumerCreateWithCFData((__bridge CFMutableDataRef)pdfData);
  CGContextRef pdfContext = CGPDFContextCreate(pdfConsumer, &pageRect, NULL);

  CGPDFContextBeginPage(pdfContext, NULL);
  CGContextTranslateCTM(pdfContext, 0, pageRect.size.height);
  for (int i = 1; i <= pageCount; i++) 
  {
    pageRef = CGPDFDocumentGetPage(pdf, i);
    CGContextTranslateCTM(pdfContext, 0, -pageHeight);
    CGContextDrawPDFPage(pdfContext, pageRef);
  }
  CGPDFContextEndPage(pdfContext);
  CGPDFContextClose(pdfContext);

  NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  NSString *documentsDirectory = [paths objectAtIndex:0];
  NSString *pdfFile = [documentsDirectory stringByAppendingPathComponent:@"destination.pdf"];

  [pdfData writeToFile: pdfFile atomically: NO];
 }

EDIT : Merging pages into one page PDF credit goes to @iPDFDev

I have three options for you probably you might get your answer in any one of them.

1)check this link. MonkeyBread

2) With Pdflab you can split, combine, reorganize Pdf's.

3) All you have to do is open the doc file in preview, apple + c copies the page that you are on, apple + n., apple + v and you have the single page pdf.

I hope these help you to some extent.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM