rabin 5 年之前
父節點
當前提交
6fd64f32bd
共有 3 個文件被更改,包括 38 次插入9 次删除
  1. 4 0
      front/api/main.py
  2. 1 1
      front/static/document.js
  3. 33 8
      service/convert.py

+ 4 - 0
front/api/main.py

@@ -309,6 +309,9 @@ class view_path(Load):
 			page = Demeter.path + 'runtime' + path + page
 		content = File.readContent(page)
 		content = content.replace('src="', 'src="' + str(static))
+		#content = content.replace('img class', 'img style="width:auto" class')
+		#content = content.replace('<div id="pf', '<div style="width:100%" id="pf')
+		#content = content.replace('<div class="pc', '<div style="width:100%" class="pc')
 		return content
 
 	def getContent(self, file, limit, uid):
@@ -339,6 +342,7 @@ class view_path(Load):
 			content = content + '<div id="demeter_document_'+n+'" style="display:none;" class="demeter_document" data-page-no="'+n+'" data-page-url="'+page_url+n+'.page"></div>'
 
 		content = content + '</div>'
+		#content = content.replace('img class', 'img style="width:auto" class')
 		#content = content + '<script>try{pdf2htmlEX.defaultViewer = new pdf2htmlEX.Viewer({});}catch(e){}</script>'
 		return content
 

+ 1 - 1
front/static/document.js

@@ -18,7 +18,6 @@
  * ================================================================================
  */
 
-//文档工具包
 var Document = 
 {
     option : {},
@@ -215,6 +214,7 @@ var Document =
         } else {
             var z = 100/b;
             $(self.option.reader).css('zoom', z + '%');
+            $('.w2').css('width', '100%');
         }
         doc.show();
         layer.close(index);

+ 33 - 8
service/convert.py

@@ -194,16 +194,15 @@ class Convert(object):
 		return False
 
 	def command(self, info):
-		File.mkdir(info['path'])
 		convert = 'cd ' + info['path'] + ' && '
 
-		if info['ext'] != '.pdf':
+		if 'pdf' not in info['ext']:
 			#convert = convert + 'libreoffice  --invisible --convert-to pdf ' + info['local']
 			convert = convert + 'soffice --headless --invisible --convert-to pdf ' + info['local']
 			convert = convert + ' && '
 		else:
 			info['pdf'] = info['local']
-		convert = convert + 'pdf2htmlEX --zoom 1 --use-cropbox 0 --no-drm 1 --split-pages 1 '
+		convert = convert + 'pdf2htmlEX --zoom 1 --hdpi 100 --vdpi 100 --use-cropbox 0 --no-drm 1 --split-pages 1 '
 		convert = convert + '--embed-css 0  --embed-javascript 0 --embed-image 0 --embed-font 1 --process-outline 0 '
 		convert = convert + '--embed-external-font 0 --dest-dir '+info['path']+' --page-filename %d.page ' + info['pdf']
 
@@ -244,12 +243,25 @@ class Convert(object):
 
 			if not File.exists(info['html']):
 			#if info:
+				File.mkdir(info['path'])
+				if 'txt' in info['ext']:
+					import chardet
+					file_path = info['local']
+					with open(file_path, 'rb') as fp:
+						file_data = fp.read()
+						result = chardet.detect(file_data)
+						fp.close()
+						if result['encoding'] != 'utf-8':
+							file_content = file_data.decode(result['encoding'])
+							f = open(file_path, 'w')
+							f.write(file_content)
+							f.close()
 				handle = self.command(info)
 				Shell.popen(handle)
 				if File.exists(info['html']):
 
-					# 截屏
-					self.cut(info)
+					# 处理图片
+					self.pic(info)
 					#self.string_switch(info['html'], "taste", "tasting")
 					# 获取有多少页
 					page = self.total(info['path'])
@@ -301,9 +313,22 @@ class Convert(object):
 
 			Demeter.curl(api, param, 'post')
 
-	def cut(self, info):
+	def pic(self, info):
 		from pdf2jpg import pdf2jpg
 		from wand.image import Image
+		width = 800
+		# 对生成的图片进行缩放
+		files = File.getFiles(info['path'])
+		if files:
+			for file in files:
+				if 'jpg' in file or 'png' in file:
+					file = info['path'] + file
+					with Image(filename=file) as img:
+						target_width, target_height = self.getSize(width, img.width, img.height)
+						img.sample(target_width, target_height)
+						img.save(filename=file)
+
+		# 生成图片
 		dest = info['html'] + '.photo'
 		result = pdf2jpg.convert_pdf2jpg(info['pdf'], dest, pages="0,1")
 
@@ -316,9 +341,9 @@ class Convert(object):
 				dest = info['html'] + '.jpg_' + i + '.jpg'
 			command = 'mv '+source+' ' + dest
 			Shell.popen(command)
-			target_width = 300
+			
 			with Image(filename=dest) as img:
-				target_width, target_height = self.getSize(target_width, img.width, img.height)
+				target_width, target_height = self.getSize(width, img.width, img.height)
 				img.sample(target_width, target_height)
 				img.save(filename=dest)
 			i = i+1