图片转成pdf下载
問題描述
實現一個查詢功能,后端根據輸入的名稱查詢數據庫,返回對應的圖片鏈接,
將圖片轉成pdf后下載
例如:
后端返回數據如下:
其中照片鏈接是base64加密后的,需要解密
后端參考:https://www.yisu.com/zixun/312294.html
查詢
const {proxy}=getCurrentInstance(); const state = reactive({value: '', // 證書編號resUrl:'', // 圖片結果url }); const onSearch = searchValue => {proxy.axios.get(`http://******************/query/${state.value}`).then((res) => {message.success('查詢成功');state.resUrl = window.atob(res.data['照片'])}).catch((err) => {if(state.value == '') message.warning('請輸入查詢編號');else message.error('查詢失敗');});};下載
需要注意的點
設置'Content-Type': 'multipart/form-data',保證格式
設置responseType: "arraybuffer" , 否則pdf可能是空白
其中用到了兩個函數
setInitImg
通過圖片鏈接轉換成file格式,由于參考的文章中用的是form表單,默認上傳file,不適用于在線圖片,因此需要先進行格式轉換
新建一個transferFile.js
download
創建a標簽下載pdf文件
新建download.js文件
頁面代碼
<template><div class="container"><div class="header-div"><h1>世界珠寶拍賣數據平臺</h1><p>WORLD JEWELRY AUCTION DATA PLATFORM</p><h2>權威 精準 可靠</h2><p>追蹤來自蘇富比、佳士得、富藝斯、邦瀚斯、嘉德、保利、匡時、瀚海等全球知名權威拍賣行的白鉆、彩鉆、紅寶石、藍寶石、祖母綠拍賣數據,基于圖表形式,為您展示各類寶石的詳盡成交數據記錄、分析成交信息。</p></div><div class="main-div"><div class="illustration"><h1>證書查詢</h1><p>高端珠寶收藏報告代表了最高標準的可靠性、一致性和完整性。我們為所有證書報告提供核對服務,讓您享受更令人放心的保證。借助證書報告核對服務,您可以方便快捷地確認證書中記載的信息與WJADP證書數據庫中存檔的信息是否一致。</p><div class="logo"></div></div><div class="search"><p>查詢證書</p><a-input v-model:value="state.value" placeholder="輸入證書編號" size="large" /><a-button type="primary" size="large" @click="onSearch">查詢</a-button></div><div class="picture" v-show="!state.isShow"><a-carousel :after-change="onChange" dot-position="bottom" arrows dots-class="slick-dots slick-thumb"><template #customPaging="props"><a><img :src="getImgUrl(props.i)" /></a></template><div v-for="item in 6" :key="item"><img :src="getImgUrl(item-1)" width='400'/></div></a-carousel></div><div class="search-data" v-show="state.isShow"><a-descriptions bordered :column="1"><a-descriptions-item label="證書編號">{{state.resForm.id}}</a-descriptions-item><a-descriptions-item label="出證日期">{{state.resForm.date}}</a-descriptions-item><a-descriptions-item label="品種">{{state.resForm.variety}}</a-descriptions-item><a-descriptions-item label="重量">{{state.resForm.weight}}</a-descriptions-item><a-descriptions-item label="證書下載"><a-button type="primary" @click="downloadFile">下載</a-button></a-descriptions-item></a-descriptions><a-image:width="400":src="state.resUrl"/></div></div><div class="footer-div"><p>需要幫助?</p><p>聯系我們</p><p>常見問題</p></div></div></template><script setup>import { reactive, getCurrentInstance } from 'vue';import { message } from 'ant-design-vue';import { setInitImg } from '../src/assets/transferFile'import { download } from '../src/assets/download'const {proxy}=getCurrentInstance();const state = reactive({value: '', // 證書編號isShow: false, // 是否顯示查詢結果resForm:{}, // 查詢結果resUrl:'', // 圖片結果url});// 點擊查詢const onSearch = searchValue => {proxy.axios.get(`http://********************/query/${state.value}`).then((res) => {message.success('查詢成功');state.resForm.id = res.data['編號']state.resForm.date = res.data['時間']state.resForm.variety = res.data['拍賣主石']state.resForm.weight = res.data['重量Ct']state.resUrl = window.atob(res.data['照片'])state.isShow = trueconsole.log(res.data)}).catch((err) => {if(state.value == '') message.warning('請輸入查詢編號');else message.error('查詢失敗');console.log(err);});};// 獲取示例圖片const baseUrl = "./assets/imgs/";const getImgUrl = i => {return require('./assets/imgs/' + i +'.jpg')};const onChange = current => {console.log(current);};// 點擊下載const downloadFile = () => {const config = {headers:{'Content-Type': 'multipart/form-data'},responseType: "arraybuffer"};const fileUrl = "http://*************************/pdf/image/to";setInitImg(state.resUrl, `${state.value}.jpg`, (file) => {console.log(file, file.name, file.size ,'file')const fileData ={'file': file,}proxy.axios.post(fileUrl,fileData,config).then((res) => {// 調用封裝好的下載函數download(res.data, 'application/pdf;charset=UTF-8', `${state.value}.pdf`) }).catch((err) => {console.log(err.message);});})};</script><style scoped lang="scss">.container {text-align: center;display: flex;flex-direction: column;// height: 100vh;}.header-div {background-color: #364d79;color: rgba(255, 253, 253, 0.788);padding: 60px 200px;h1 {color: #fff;font-size: 40px;}h2 {color: rgba(255, 253, 253, 0.788);font-size: 25px;margin: 50px 0 20px 0;}}.main-div {padding: 30px 100px;display: flex;flex-direction: column;align-items: center;.illustration {display: flex;flex-direction: column;align-items: center;h1 {margin: 20px;}p {width: 600px}.logo {width: 100px;height: 100px;margin: 20px 0;background: url("./assets/imgs/logo.jpg") no-repeat center;background-size: 100%;}}.search {display: flex;align-items: center;justify-content: center;p {margin: 0;// width: 100px;font-weight: 800;}input {width: 300px;margin: 30px;}button {background: black;border: none;// width: 100px;}}.picture {width: 700px}.search-data {width: 700px}}.footer-div {background-color: #364d79;padding: 20px;color: #fff;text-align: center;p {display: block;margin: 10px;}}.download-pic {display: flex;justify-content: space-between;.pic {display: flex;flex-direction: column;}}.ant-carousel :deep(.slick-dots) {position: relative;height: auto;}.ant-carousel :deep(.slick-slide img) {border: 5px solid #fff;display: block;margin: auto;// max-width: 80%;}.ant-carousel :deep(.slick-arrow) {display: none !important;}.ant-carousel :deep(.slick-thumb) {bottom: 0px;}.ant-carousel :deep(.slick-thumb li) {width: 60px;height: 45px;}.ant-carousel :deep(.slick-thumb li img) {width: 100%;height: 100%;filter: grayscale(100%);display: block;}.ant-carousel :deep .slick-thumb li.slick-active img {filter: grayscale(0%);} </style>結果
查詢前
查詢后
總結
- 上一篇: Matlab中的poly2trellis
- 下一篇: 嵌入式系统裸机程序开发与调试