下载openoffice 并安装
//pdf.js 案例
https://mozilla.github.io/pdf.js/examples/index.html#interactive-examples
//openoffice 连接不上 进入安装目录 cmd 运行以下命令
soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard
< ! -- openoffice-- >
< dependency>
< groupId> com. artofsolving< / groupId>
< artifactId> jodconverter< / artifactId>
< version> 2.2 .1 < / version>
< / dependency>
import com. artofsolving. jodconverter. DefaultDocumentFormatRegistry ;
import com. artofsolving. jodconverter. DocumentConverter ;
import com. artofsolving. jodconverter. DocumentFormat ;
import com. artofsolving. jodconverter. openoffice. connection. OpenOfficeConnection ;
import com. artofsolving. jodconverter. openoffice. connection. SocketOpenOfficeConnection ;
import com. artofsolving. jodconverter. openoffice. converter. StreamOpenOfficeDocumentConverter ;
import java. io. * ;
import java. net. HttpURLConnection ;
import java. net. URL ;
import java. net. URLConnection ;
public class OfficeTools {
private static final String DEFAULT_SUFFIX = "pdf" ;
private static final Integer OPENOFFICE_PORT = 8100 ;
public static InputStream covert ( InputStream is, String suffix) throws Exception {
ByteArrayOutputStream out = new ByteArrayOutputStream ( ) ;
OpenOfficeConnection conn = new SocketOpenOfficeConnection ( OPENOFFICE_PORT ) ;
conn. connect ( ) ;
DocumentConverter converter = new StreamOpenOfficeDocumentConverter ( conn) ;
DefaultDocumentFormatRegistry formatReg = new DefaultDocumentFormatRegistry ( ) ;
DocumentFormat targetFormat = formatReg. getFormatByFileExtension ( DEFAULT_SUFFIX ) ;
DocumentFormat sourceFormat = formatReg. getFormatByFileExtension ( suffix) ;
converter. convert ( is, sourceFormat, out, targetFormat) ;
conn. disconnect ( ) ;
return outputStreamConvertInputStream ( out) ;
}
public static ByteArrayInputStream outputStreamConvertInputStream ( final OutputStream out) throws Exception {
ByteArrayOutputStream os = ( ByteArrayOutputStream ) out;
return new ByteArrayInputStream ( os. toByteArray ( ) ) ;
}
}
import com. jianmu. constant. SysApiConstant ;
import com. jianmu. exception. ErrorException ;
import com. jianmu. service. i. UploadService ;
import com. jianmu. tools. OfficeTools ;
import com. jianmu. tools. OssFileTools ;
import com. jianmu. tools. log. LogTools ;
import org. springframework. beans. factory. annotation. Autowired ;
import org. springframework. web. bind. annotation. GetMapping ;
import org. springframework. web. bind. annotation. RequestMapping ;
import org. springframework. web. bind. annotation. RequestParam ;
import org. springframework. web. bind. annotation. RestController ;
import javax. servlet. http. HttpServletResponse ;
import java. io. InputStream ;
import java. io. OutputStream ;
@RestController
@RequestMapping ( SysApiConstant . OPEN_MOBILE_API + "office" )
public class MbOpenOfficeApi {
private final UploadService uploadService;
@Autowired
public MbOpenOfficeApi ( UploadService uploadService) {
this . uploadService = uploadService;
}
@GetMapping ( "/preview" )
public void preview ( @RequestParam ( "url" ) String url, HttpServletResponse response) {
String suffix = url. substring ( url. lastIndexOf ( "." ) + 1 ) ;
String defaultConvertSuffix = "doc" ;
if ( ! defaultConvertSuffix. equals ( suffix) ) {
throw new ErrorException ( "文件格式不支持预览" ) ;
}
try ( InputStream is = OssFileTools . getOssFileInputStream ( url, this . uploadService. getUploadConfig ( ) ) ;
InputStream in = OfficeTools . covert ( is, suffix) ;
OutputStream outputStream = response. getOutputStream ( ) ) {
byte [ ] buff = new byte [ 1024 ] ;
int n;
while ( ( n = in. read ( buff) ) != - 1 ) {
outputStream. write ( buff, 0 , n) ;
}
outputStream. flush ( ) ;
} catch ( Exception e) {
LogTools . err ( e) ;
}
}
}
<! DOCTYPE html >
< html lang = " en" >
< head>
< meta charset = " UTF-8" >
< meta http-equiv = " X-UA-Compatible" content = " IE=edge" >
< meta name = " viewport" content = " width=device-width, initial-scale=1.0" >
< script src = " https://cdnjs.cloudflare.com/ajax/libs/pdf.js/3.3.122/pdf.min.js" > </ script>
</ head>
< body>
< canvas id = " the-canvas" > </ canvas>
< script>
var pdfjsLib = window[ 'pdfjs-dist/build/pdf' ] ;
var loadingTask = pdfjsLib. getDocument (
"http://192.168.3.56:8700/vt/api/mb/open/office/preview?url=upload/doc/20230217095659/1626400282622058498.doc"
) ;
loadingTask. promise. then ( function ( pdf ) {
console. log ( 'PDF loaded' ) ;
var pageNumber = 1 ;
pdf. getPage ( pageNumber) . then ( function ( page ) {
console. log ( 'Page loaded' ) ;
var scale = 1.5 ;
var viewport = page. getViewport ( {
scale : scale
} ) ;
var canvas = document. getElementById ( 'the-canvas' ) ;
var context = canvas. getContext ( '2d' ) ;
canvas. height = viewport. height;
canvas. width = viewport. width;
var renderContext = {
canvasContext : context,
viewport : viewport
} ;
var renderTask = page. render ( renderContext) ;
renderTask. promise. then ( function ( ) {
console. log ( 'Page rendered' ) ;
} ) ;
} ) ;
} , function ( reason ) {
console. error ( reason) ;
} ) ;
</ script>
</ body>
</ html>