Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to generate PDF format for output in H5 interface

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

In this article Xiaobian for you to introduce in detail "H5 interface how to generate PDF format for output", the content is detailed, the steps are clear, the details are handled properly, I hope this "H5 interface how to generate PDF format for output" article can help you solve your doubts, following the editor's ideas slowly in-depth, together to learn new knowledge.

The H5 interface needs two forms to generate PDF:

1: preservation in the form of pictures.

The main thing is to intercept all the content of the H 5 interface, which is in the form of pictures, and then generate PDF format.

2: intercept H5 interface strings and pictures

Save all the words and pictures, assemble them, and then save the PDF format in different styles.

The first method is only a simple operation, but can not meet the needs, can only choose the second.

But the second to intercept and save data and pictures, write a set of words, a lot of work.

Use the UIWebView in the system

IOS developers are all too familiar with it, , they all use this method.

[UIWebView loadHTMLString:@ "" baseURL:@ ""]

Load all the text and pictures in the H 5 interface.

Then use the following method to save the data locally

This is the UIWebVIew extension class. Just write it yourself.

-(NSData *) converToPDF {

UIViewPrintFormatter * fmt = [self viewPrintFormatter]

UIPrintPageRenderer * render = [[UIPrintPageRenderer alloc] init]

[render addPrintFormatter:fmt startingAtPageAtIndex:0]

CGRect page

Page.origin.x=0

Page.origin.y=0

Page.size.width=600

Page.size.height=768

CGRect printable=CGRectInset (page, 50,50)

[render setValue: [NSValue valueWithCGRect:page] forKey:@ "paperRect"]

[render setValue: [NSValue valueWithCGRect:printable] forKey:@ "printableRect"]

NSMutableData * pdfData = [NSMutableData data]

UIGraphicsBeginPDFContextToData (pdfData, CGRectZero, nil)

For (NSInteger iTun0; I < [render numberOfPages]; iTunes +)

{

UIGraphicsBeginPDFPage ()

CGRect bounds = UIGraphicsGetPDFContextBounds ()

[render drawPageAtIndex:i inRect:bounds]

}

UIGraphicsEndPDFContext ()

Return pdfData

}

/ / Click Save to call the above method

-(void) savePDF

{

NSData * data = [_ webView converToPDF]; call the above method

NSString * path = [NSHomeDirectory () stringByAppendingPathComponent: [NSString stringWithFormat:@ "Documents/testFile.pdf"]]

BOOL result = [data writeToFile:path atomically:YES]

[MBProgressHUD hideHUD]

If (result) {

"saved successfully"

} else {

"Save failed"

}

Get the path locally to display PDF

NSURL * pdfURL = [NSURL fileURLWithPath:path]

NSURLRequest * request = [NSURLRequest requestWithURL:pdfURL]

[self.webView setScalesPageToFit:YES]

[self.webView loadRequest:request]

}

Then open it locally and ok it.

Read this, the "H5 interface how to generate PDF format for output" article has been introduced, want to master the knowledge of this article also need to practice and use in order to understand, if you want to know more about the article, welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report