2022年6月23日16:11:17
默认是请求明文的pdf文件路径,但是因为是个人信息文件,有规律的文件格式,就有被拉去文件的问题,所以加密请求
$.ajax({
url: "{{URL::to('/getPdf')}}",
dataType: "json",
data: {
code: "{{$code}}"
},
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
},
type: "post",
cache: false,
success: function (response) {
//$("#loading").css('display', 'none');
if (response.code == 200) {
pdfjsLib.GlobalWorkerOptions.workerSrc = '{{URL::asset('/static/tjbg/js/pdfjs-2.2/build/pdf.worker.js')}}';
var loadingTask = pdfjsLib.getDocument(response.data);
loadingTask.promise.then(function (pdf) {
for (var i = 1; i <= pdf.numPages; i++) {
pdf.getPage(i).then(function (page) {
var scale = 1.5;
var viewport = page.getViewport({scale: scale,});
// Support HiDPI-screens.
var outputScale = window.devicePixelRatio || 1;
var canvas = document.createElement('canvas');
var context = canvas.getContext('2d');
pdfDiv.appendChild(canvas);
canvas.width = Math.floor(viewport.width * outputScale);
canvas.height = Math.floor(viewport.height * outputScale);
canvas.style.width = '100%';
canvas.style.height = '100%';
var transform = outputScale !== 1
? [outputScale, 0, 0, outputScale, 0, 0]
: null;
var renderContext = {
canvasContext: context,
transform: transform,
viewport: viewport
};
page.render(renderContext);
});
}
});
} else {
layer.msg(response.msg);
}
}
});
code就是加密的key,只要是双向加密解密的算法都可以,建议使用openssl的算法
if (empty($code)) {
throw new Exception('文件编号不能为空');
}
$file_name = decrypt($code);
$file = CommonService::getUrl($file_name, false);
$pdf = new SplFileObject($file, 'r');
$data = '';
while (!$pdf->eof()) {
$data .= $pdf->fgets();
}
$base64 = base64_encode($data);
return 'data:application/pdf;base64,' . $base64;
$file = CommonService::getUrl($file_name, false); 只是一个返回文件的路径
异步请求的就是一个base64的格式的字符流,所以就可以做好加密解密问题