-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPDF.cpp
More file actions
65 lines (59 loc) · 1.78 KB
/
PDF.cpp
File metadata and controls
65 lines (59 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include "PDF.h"
#include "DocumentContainer.h"
#include <iostream>
#include <sstream>
#include <fstream>
#include <format>
#include <windows.h>
#include <litehtml/document.h>
#include "Action.h"
#include "Helper.h"
PDF::PDF()
{
//https://www.princexml.com/
dpi = GetDpiForSystem() / 96.0;
pdfWriter.StartPDF("out.pdf", ePDFVersion13);
}
PDF::~PDF()
{
}
void PDF::start(const std::string& htmlPath)
{
std::stringstream ss{};
std::ifstream(htmlPath, std::ios::binary) >> ss.rdbuf();
auto html = ss.str();
auto docContainer = new DocumentContainer(this);
auto doc = litehtml::document::createFromString(html, docContainer);
int best_width = doc->render(viewWidth);
litehtml::position clip(0, 0, viewWidth, INT_MAX);
doc->draw((litehtml::uint_ptr)nullptr, 0, 0, &clip);
std::vector<Action*> temp;
auto pageIndex{ 0 };
std::copy_if(actions.begin(), actions.end(), std::back_inserter(temp),
[&pageIndex](Action* a) {return a->pageIndex == pageIndex; });
if (temp.empty()) {
LOG("没有发现需要写入PDF的元素");
return;
}
while (!temp.empty()) {
PDFPage* page = new PDFPage();
page->SetMediaBox(PDFRectangle(0, 0, width, height)); //A4 纸张尺寸,宽度:595 点 ≈ 210 mm,高度:842 点 ≈ 297 mm
PageContentContext* ctx = pdfWriter.StartPageContentContext(page);
for (size_t i = 0; i < temp.size(); i++)
{
temp[i]->paint(ctx);
}
pdfWriter.EndPageContentContext(ctx);
pdfWriter.WritePageAndRelease(page);
pageIndex += 1;
temp.clear();
std::copy_if(actions.begin(), actions.end(), std::back_inserter(temp),
[&pageIndex](Action* a) {return a->pageIndex == pageIndex; });
}
for (size_t i = 0; i < actions.size(); i++)
{
delete actions[i];
}
actions.clear();
pdfWriter.EndPDF();
}