Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Sign in
Toggle navigation
T
tanpu-community
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
探普后端
tanpu-community
Commits
185605ed
Commit
185605ed
authored
Oct 26, 2021
by
张辰
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
1
parent
5c6f6dae
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
26 additions
and
25 deletions
+26
-25
SearchController.java
...java/com/tanpu/community/controller/SearchController.java
+7
-3
PagePdfGenUtils.java
...c/main/java/com/tanpu/community/util/PagePdfGenUtils.java
+19
-22
No files found.
community-service/src/main/java/com/tanpu/community/controller/SearchController.java
View file @
185605ed
...
...
@@ -16,6 +16,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import
org.springframework.web.bind.annotation.*
;
import
javax.annotation.Resource
;
import
java.io.IOException
;
import
java.util.List
;
@RestController
...
...
@@ -63,8 +64,11 @@ public class SearchController {
@GetMapping
(
"/testGenPdf"
)
public
CommonResp
testGenPdf
(
@RequestParam
(
"url"
)
String
url
)
{
PagePdfGenUtils
.
genPdf
(
url
);
return
CommonResp
.
success
();
try
{
PagePdfGenUtils
.
genPdf
(
url
);
return
CommonResp
.
success
();
}
catch
(
Throwable
e
)
{
return
CommonResp
.
error
(
"9999"
,
e
.
getMessage
());
}
}
}
community-service/src/main/java/com/tanpu/community/util/PagePdfGenUtils.java
View file @
185605ed
...
...
@@ -9,34 +9,31 @@ import com.ruiyun.jvppeteer.options.LaunchOptionsBuilder;
import
com.ruiyun.jvppeteer.options.PDFOptions
;
import
lombok.extern.slf4j.Slf4j
;
import
java.io.IOException
;
import
java.util.ArrayList
;
@Slf4j
public
class
PagePdfGenUtils
{
public
static
void
genPdf
(
String
url
)
{
try
{
BrowserFetcher
.
downloadIfNotExist
(
null
);
ArrayList
<
String
>
argList
=
new
ArrayList
<>();
LaunchOptions
options
=
new
LaunchOptionsBuilder
()
.
withExecutablePath
(
"/usr/bin/google-chrome"
)
.
withArgs
(
argList
)
.
withHeadless
(
true
)
.
build
();
public
static
void
genPdf
(
String
url
)
throws
Throwable
{
BrowserFetcher
.
downloadIfNotExist
(
null
);
ArrayList
<
String
>
argList
=
new
ArrayList
<>();
LaunchOptions
options
=
new
LaunchOptionsBuilder
()
.
withExecutablePath
(
"/usr/bin/google-chrome"
)
.
withArgs
(
argList
)
.
withHeadless
(
true
)
.
build
();
argList
.
add
(
"--no-sandbox"
);
argList
.
add
(
"--disable-setuid-sandbox"
);
Browser
browser
=
Puppeteer
.
launch
(
options
);
Page
page
=
browser
.
newPage
();
page
.
goTo
(
url
);
argList
.
add
(
"--no-sandbox"
);
argList
.
add
(
"--disable-setuid-sandbox"
);
Browser
browser
=
Puppeteer
.
launch
(
options
);
Page
page
=
browser
.
newPage
();
page
.
goTo
(
url
);
PDFOptions
pdfOptions
=
new
PDFOptions
();
pdfOptions
.
setPath
(
"/biz/tmp/test.pdf"
);
byte
[]
pdfBytes
=
page
.
pdf
(
pdfOptions
);
page
.
close
();
browser
.
close
();
}
catch
(
Exception
e
)
{
log
.
error
(
"error in genPdf for {}, error: {}"
,
url
,
e
);
}
PDFOptions
pdfOptions
=
new
PDFOptions
();
pdfOptions
.
setPath
(
"/usr/src/myapp/test.pdf"
);
byte
[]
pdfBytes
=
page
.
pdf
(
pdfOptions
);
page
.
close
();
browser
.
close
();
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment