Skip to content

Commit 889d09c

Browse files
committed
✨ 终于支持导出pdf分页了
1 parent bc26ba4 commit 889d09c

File tree

3 files changed

+81
-37
lines changed

3 files changed

+81
-37
lines changed

Mac/View/MPreviewView.swift

+80-19
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import Carbon.HIToolbox
2+
import PDFKit
23
import WebKit
34

45
#if os(iOS)
@@ -83,30 +84,90 @@ class MPreviewView: WKWebView, WKUIDelegate, WKNavigationDelegate {
8384

8485
public func exportPdf() {
8586
guard let vc = ViewController.shared() else { return }
87+
// 获取 WKWebView 的内容大小
88+
var a4Size = CGSize(width: 800, height: 1120)
89+
if UserDefaultsManagement.isOnExportPPT {
90+
a4Size = CGSize(width: 1536, height: 957)
91+
}
8692

87-
if #available(macOS 11.0.0, *) {
88-
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
89-
let config = WKPDFConfiguration()
90-
// Render the PDF
91-
super.createPDF(configuration: config) { result in
92-
switch result {
93-
case .success(let data):
94-
if let path = NSSearchPathForDirectoriesInDomains(.downloadsDirectory, .userDomainMask, true).first {
95-
let currentName = self.note?.getTitle()
96-
let filePath: String = path + "/" + (currentName ?? "MiaoYan") + ".pdf"
97-
try! data.write(to: URL(fileURLWithPath: filePath))
98-
vc.toastExport(status: true)
99-
}
93+
let pageWidth = a4Size.width
94+
let pageHeight = a4Size.height
95+
let maxHeight: CGFloat = 14355.0
96+
97+
super.frame.size.width = pageWidth
98+
99+
super.evaluateJavaScript("document.body.scrollHeight", completionHandler: { height, error in
100+
guard let height = height as? CGFloat, error == nil else {
101+
vc.toastExport(status: false)
102+
return
103+
}
104+
if #available(macOS 11.0, *) {
105+
let numberOfParts = Int(ceil(height / maxHeight))
106+
let newDocument = PDFDocument()
107+
108+
// 设置 PDF 分页
109+
func processPart(_ partIndex: Int) {
110+
let partY = CGFloat(partIndex) * maxHeight
111+
let partHeight = min(maxHeight, height - partY)
112+
113+
let pdfConfiguration = WKPDFConfiguration()
114+
pdfConfiguration.rect = CGRect(x: 0, y: partY, width: super.bounds.width, height: partHeight)
115+
116+
super.createPDF(configuration: pdfConfiguration) { result in
117+
switch result {
118+
case .success(let pdfData):
119+
// 使用 PDFKit 进行分页
120+
121+
let pdfDocument = PDFDocument(data: pdfData)
122+
123+
for i in 0 ..< (pdfDocument?.pageCount ?? 0) {
124+
guard let page = pdfDocument?.page(at: i) else {
125+
return
126+
}
127+
128+
let pageBounds = page.bounds(for: .cropBox)
129+
let subPageCount = Int(ceil(pageBounds.height / a4Size.height))
130+
131+
for j in 0 ..< subPageCount {
132+
let subPageRect = CGRect(x: 0, y: CGFloat(subPageCount - j - 1) * a4Size.height, width: pageWidth, height: a4Size.height)
100133

101-
case .failure(let error):
102-
print(error)
103-
vc.toastExport(status: false)
134+
guard let subPage = page.copy() as? PDFPage else {
135+
return
136+
}
137+
138+
subPage.setBounds(subPageRect, for: .cropBox)
139+
newDocument.insert(subPage, at: newDocument.pageCount)
140+
}
141+
}
142+
143+
if partIndex < numberOfParts - 1 {
144+
processPart(partIndex + 1)
145+
} else {
146+
if let newPDFData = newDocument.dataRepresentation() {
147+
if let path = NSSearchPathForDirectoriesInDomains(.downloadsDirectory, .userDomainMask, true).first {
148+
vc.toastExport(status: true)
149+
let currentName = self.note?.getTitle()
150+
let filePath: String = path + "/" + (currentName ?? "MiaoYan") + ".pdf"
151+
try! newPDFData.write(to: URL(fileURLWithPath: filePath))
152+
}
153+
154+
} else {}
155+
}
156+
157+
case .failure:
158+
vc.toastExport(status: false)
159+
return
160+
}
104161
}
105162
}
163+
164+
processPart(0)
165+
166+
} else {
167+
// Fallback on earlier versions
168+
vc.toastExport(status: false)
106169
}
107-
} else {
108-
vc.toastExport(status: false)
109-
}
170+
})
110171
}
111172

112173
public func slideTo(index: Int) {

Mac/ViewController.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -2552,7 +2552,7 @@ class ViewController:
25522552
UserDefaultsManagement.isOnExportPPT = true
25532553
toast(message: NSLocalizedString("🙊 Starting export~", comment: ""))
25542554
enableMiaoYanPPT()
2555-
DispatchQueue.main.asyncAfter(deadline: .now() + 2.5) {
2555+
DispatchQueue.main.asyncAfter(deadline: .now() + 4) {
25562556
self.editArea.markdownView?.exportPdf()
25572557
UserDefaultsManagement.isOnExport = false
25582558
UserDefaultsManagement.isOnExportPPT = false

Resources/DownView.bundle/ppt/dist/reveal.css

-17
Original file line numberDiff line numberDiff line change
@@ -2024,32 +2024,15 @@ html.print-pdf .reveal .slides .pdf-page {
20242024
overflow: hidden;
20252025
z-index: 1;
20262026
page-break-after: always;
2027-
border: 8px solid #d1d6ee;
20282027
border-top: none;
20292028
box-sizing: border-box;
20302029
background-color: #fff !important;
20312030
}
20322031

2033-
html.print-pdf .reveal .slides .pdf-page:first-child {
2034-
border-top: 8px solid #d1d6ee;
2035-
}
2036-
2037-
html.print-pdf .reveal-viewport {
2038-
background-color: #d1d6ee;
2039-
}
20402032

20412033
@media (prefers-color-scheme: dark) {
20422034
html.print-pdf .reveal .slides .pdf-page {
20432035
background-color: #21262b !important;
2044-
border: 8px solid #fff;
2045-
}
2046-
2047-
html.print-pdf .reveal .slides .pdf-page:first-child {
2048-
border-top: 8px solid #fff;
2049-
}
2050-
2051-
html.print-pdf .reveal-viewport {
2052-
background-color: #fff;
20532036
}
20542037
}
20552038

0 commit comments

Comments
 (0)