The front-end vue is based on html2canva jspdf to implement watermarking on the front-end page and export the page PDF

With the development of technology, the complexity of development is getting higher and higher. The traditional development method makes a system into a whole application. It often happens that a small change or the addition of a small function may cause the overall logic to change. The modification affects the whole body. Through component development, individual development and maintenance can be effectively achieved, and they can be combined at will. Greatly improve low development efficiency and reduce maintenance costs.

Componentization is the only way for any front-end application with complex business scenarios and products after multiple iterations. Componentization requires more than just the module splitting and decoupling seen on the surface. There is also a lot of work behind it to support componentization, such as module splitting strategies based on business characteristics, interaction methods between modules, and building systems. etc.

The components introduced in this article are:

Based on html2canva jspdf, implement watermarking on front-end pages and export page PDF; to download the complete code, please visit the uni-app plug-in market address: https://ext.dcloud.net.cn/plugin?id=12761

 For more information on front-end components, please follow the WeChat public account: Front-end component development

The rendering is as follows:

2c362990ac7ef50bb8e6daeaebeadfe7.png

The code is implemented as follows:

# Instructions

```How to use

<!--Introduce and export pdf plug-in-->

<script type="text/javascript" src="./static/html2canvas.js"></script>

<script type="text/javascript" src="./static/jspdf.debug.js"></script>

//Call the page watermark method

this.watermark({

"watermark_txt": "China Guangdong cc.gd"

});

//Call the download pdf report method

downKcReport();

```

#### HTML code part

```html

<template>

<view class="content">

<view class="exportPdfBtn" @click="downKcReport()">导出PDF</view>

<!-- Visualization-->

<view style="background-color: white; border-radius: 4px; margin-top: -4px;">

<!-- Column chart stacking-->

<h4 style="margin-left: 12px; font-size: 16px; height: 30px; line-height: 30px;"> Column chart stacking

</h4>

<div class="mui-content-padded">

<div id="dsj_zscq" style="width: calc(100vw - 40px); height: 280px;"></div>

</div>

<!-- Line chart-->

<h4 style="margin-left: 12px; font-size: 16px; height: 30px; line-height: 30px;"> Line chart

</h4>

<div class="mui-content-padded">

<div id="dsj_cxcg" style="width: calc(100vw - 40px); margin: -18px 0px; height: 280px;">

</div>

</div>

<!-- Bar chart + Line chart -->

<h4 style="margin-left: 12px; font-size: 16px; height: 30px; line-height: 30px; margin-top: 0px;">

Bar chart + line chart</h4>

<div class="mui-content-padded">

<div id="dsj_zlhjl" style="width: calc(100vw - 40px); height: 290px;"></div>

</div>

<!-- Effective intellectual property structure -->

<h4 style="margin-left: 12px; font-size: 16px; height: 30px; line-height: 30px; margin-top: 0px;">

Pie chart</h4>

<div class="mui-content-padded">

<div id="dsj_zscqjg" style="width: calc(100vw - 40px); height: 306px;"></div>

</div>

<!-- Patented iPC structure-->

<h4 style="margin-left: 12px; font-size: 16px; height: 30px; line-height: 30px; margin-top: 0px;">

Tree structure diagram</h4>

<div class="mui-content-padded">

<div id="dsj_zlipc_content"

style="margin-left: -10px;  width: calc(100vw - 40px); height: auto; margin-bottom: 30px;">

</div>

</div>

<!-- Histogram-->

<h4 style="margin-left: 12px; font-size: 16px; height: 30px; line-height: 30px; margin-top: 0px;">

Bar chart</h4>

<div class="mui-content-padded">

<div id="dsj_zljz" style="width: calc(100vw - 40px); height: 280px;"></div>

</div>

<!-- Patent legal status distribution-->

<h4 style="margin-left: 12px; font-size: 16px; height: 30px; line-height: 30px; margin-top: 0px;">

Pie chart</h4>

<div class="mui-content-padded">

<div id="dsj_zlfl" style="width: calc(100vw - 40px); height: 280px;"></div>

</div>

<!--Patent life distribution-->

<h4 style="margin-left: 12px; font-size: 16px; height: 30px; line-height: 30px; margin-top: 0px;">

Bar chart</h4>

<div class="mui-content-padded">

<div id="dsj_zlsm" style="width: calc(100vw - 40px); height: 280px;"></div>

</div>

<!-- Tree structure diagram -->

<h4 style="margin-left: 12px; font-size: 16px; height: 30px; line-height: 30px; margin-top: 0px;">

Tree structure diagram</h4>

<div class="mui-content-padded">

<div id="dsj_zllhsq" style="width: calc(100vw - 40px); height: auto; margin-bottom: 30px;">

</div>

</div>

</view>

</view>

</template>

```

#### JS code (introducing components to fill in data)

```javascript

<script>

import myIndex from '../index/index.js'

export default {

data() {

return {

title: 'Hello'

}

},

mounted() {

// Refresh Echart data

this.refreshEchartData();

},

onLoad() {

// How to add watermark to page

this.watermark({

"watermark_txt": "China Guangdong cc.gd"

});

},

methods: {

// How to add watermark to page

watermark(settings) {

//default setting

var defaultSettings = {

watermark_txt: "text",

watermark_x: 20, //X-axis coordinate of watermark starting position

watermark_y: 80, //Y-axis coordinate of watermark starting position

watermark_rows: 80, //Number of watermark rows

watermark_cols: 20, //Number of watermark columns

watermark_x_space: 80, //Watermark x-axis interval

watermark_y_space: 102, //Watermark y-axis interval

watermark_color: '#aaa', //watermark font color

watermark_alpha: 0.4, //watermark transparency

watermark_fontsize: '14px', //watermark font size

watermark_font: 'Microsoft Yahei', //watermark font

watermark_width: 180, //Watermark width

watermark_height: 80, //watermark length

watermark_angle: 20 //Watermark tilt degree

};

//Replace the default value with configuration items, similar to jquery.extend

if (arguments.length === 1 && typeof arguments[0] === "object") {

console.log("arguments = " + JSON.stringify(arguments[0]));

// Get parameter configuration

var src = arguments[0];

for (let key in src) {

if (src[key] && defaultSettings[key] && src[key] === defaultSettings[key])

continue;

else if (src[key])

defaultSettings[key] = src[key];

}

}

var oTemp = document.createDocumentFragment();

//Get the maximum width of the page

var page_width = Math.max(document.body.scrollWidth, document.body.clientWidth);

var cutWidth = page_width * 0.0150;

var page_width = page_width - cutWidth;

//Get the maximum height of the page

var page_height = Math.max(document.body.scrollHeight, document.body.clientHeight) + 2860;

// var page_height = document.body.scrollHeight+document.body.scrollTop;

//If the number of watermark columns is set to 0, or the number of watermark columns is set too large and exceeds the maximum width of the page, recalculate the number of watermark columns and the watermark x-axis interval

if (defaultSettings.watermark_cols == 0 || (parseInt(defaultSettings.watermark_x + defaultSettings

.watermark_width * defaultSettings.watermark_cols + defaultSettings.watermark_x_space * (

defaultSettings.watermark_cols - 1)) > page_width)) {

defaultSettings.watermark_cols = parseInt((page_width - defaultSettings.watermark_x + defaultSettings

.watermark_x_space) / (defaultSettings.watermark_width + defaultSettings

.watermark_x_space));

defaultSettings.watermark_x_space = parseInt((page_width - defaultSettings.watermark_x -

defaultSettings

.watermark_width * defaultSettings.watermark_cols) / (defaultSettings.watermark_cols - 1));

}

//If the number of watermark lines is set to 0, or the number of watermark lines is set too large and exceeds the maximum length of the page, recalculate the number of watermark lines and the watermark y-axis interval

if (defaultSettings.watermark_rows == 0 || (parseInt(defaultSettings.watermark_y + defaultSettings

.watermark_height * defaultSettings.watermark_rows + defaultSettings.watermark_y_space * (

defaultSettings.watermark_rows - 1)) > page_height)) {

defaultSettings.watermark_rows = parseInt((defaultSettings.watermark_y_space + page_height -

defaultSettings

.watermark_y) / (defaultSettings.watermark_height + defaultSettings.watermark_y_space));

defaultSettings.watermark_y_space = parseInt(((page_height - defaultSettings.watermark_y) -

defaultSettings

.watermark_height * defaultSettings.watermark_rows) / (defaultSettings.watermark_rows - 1));

}

var x;

var and;

for (var i = 0; i < defaultSettings.watermark_rows; i++) {

y = defaultSettings.watermark_y + (defaultSettings.watermark_y_space + defaultSettings

.watermark_height) * i;

for (var j = 0; j < defaultSettings.watermark_cols; j++) {

x = defaultSettings.watermark_x + (defaultSettings.watermark_width + defaultSettings

.watermark_x_space) *

j;

var mask_div = document.createElement('div');

mask_div.id = 'mask_div' + i + j;

mask_div.className = 'mask_div';

mask_div.appendChild(document.createTextNode(defaultSettings.watermark_txt));

//Set the watermark div to display tiltedly

mask_div.style.webkitTransform = "rotate(-" + defaultSettings.watermark_angle + "deg)";

mask_div.style.MozTransform = "rotate(-" + defaultSettings.watermark_angle + "deg)";

mask_div.style.msTransform = "rotate(-" + defaultSettings.watermark_angle + "deg)";

mask_div.style.OTransform = "rotate(-" + defaultSettings.watermark_angle + "deg)";

mask_div.style.transform = "rotate(-" + defaultSettings.watermark_angle + "deg)";

mask_div.style.visibility = "";

mask_div.style.position = "absolute";

mask_div.style.left = x + 'px';

mask_div.style.top = y + 'px';

mask_div.style.overflow = "hidden";

mask_div.style.zIndex = "9999";

mask_div.style.pointerEvents = 'none'; //pointer-events:none makes the watermark not block the click event of the page

//mask_div.style.border="solid #eee 1px";

mask_div.style.opacity = defaultSettings.watermark_alpha;

mask_div.style.fontSize = defaultSettings.watermark_fontsize;

mask_div.style.fontFamily = defaultSettings.watermark_font;

mask_div.style.color = defaultSettings.watermark_color;

mask_div.style.textAlign = "center";

mask_div.style.width = defaultSettings.watermark_width + 'px';

mask_div.style.height = defaultSettings.watermark_height + 'px';

mask_div.style.display = "block";

oTemp.appendChild(mask_div);

};

};

document.body.appendChild(oTemp);

},

// Download pdf report

downKcReport() {

html2canvas(

document.body, {

scale: 2,

dpi: 192, //Export pdf resolution

unrendered: function(canvas) {

var contentWidth = canvas.width;

var contentHeight = canvas.height;

//One page of pdf displays the canvas height generated by the html page;

var pageHeight = contentWidth / 592.28 * 841.89;

//The html page height of the pdf is not generated

var leftHeight = contentHeight;

//pdf page offset

var position = 0;

//The width and height of the canvas generated by the html page in the pdf image (size of a4 paper [595.28,841.89])

var imgWidth = 595.28;

var imgHeight = 592.28 / contentWidth * contentHeight;

var pageData = canvas.toDataURL('image/jpeg', 1.0);

var pdf = new jsPDF('', 'pt', 'a4');

//There are two heights that need to be distinguished, one is the actual height of the html page, and the page height of the generated pdf (841.89)

//When the content does not exceed the range displayed on one PDF page, no paging is required

if (leftHeight < pageHeight) {

pdf.addImage(pageData, 'JPEG', 0, 0, imgWidth, imgHeight);

} else {

while (leftHeight > 0) {

pdf.addImage(pageData, 'JPEG', 0, position, imgWidth, imgHeight)

leftHeight -= pageHeight;

position -= 841.89;

//Avoid adding blank pages

if (leftHeight > 0) {

pdf.addPage();

}

}

}

pdf.save(name + '.pdf');

console.log('Export pdf successfully');

},

//Set the background to white (default is black)

background: "#fff"

})

},

refreshEchartData() {

//The returned data needs to be converted into Json format using the json conversion format tool

let result = {

"data": {

"unionCompanyNum": 4,

"goldList": [{

"isNewRecord": true,

"comId": "1",

"socialCode": "123456",

"fmsqYx": "1",

"cfmwyxzscqsl": "1",

"years": "1",

"yxzscqsl": "1",

"dnzlhjl": "1"

}],

"patentLifeList": [{

"isNewRecord": true,

"comId": "1",

"socialCode": "123456",

"percentage": "1",

"quantity": "1",

"scope": "1-3",

"totalNum": "1"

}, {

"isNewRecord": true,

"comId": "1",

"socialCode": "123456",

"percentage": "1",

"quantity": "1",

"scope": "3-5",

"totalNum": "2"

}, {

"isNewRecord": true,

"comId": "1",

"socialCode": "123456",

"percentage": "1",

"quantity": "3",

"scope": "5-10",

"totalNum": "3"

}, {

"isNewRecord": true,

"comId": "1",

"socialCode": "123456",

"percentage": "1",

"quantity": "3",

"scope": "10-",

"totalNum": "4"

}],

"patentValueList": [{

"isNewRecord": true,

"comId": "1",

"socialCode": "123456",

"fmgbZs": "1",

"fmsqYx": "1",

"syxxYx": "1",

"wgsjYx": "1",

"scope": "0-5",

"totalNum": "1",

"percentageHighRights": "1",

"percentageHigh": "1",

"dspw": "1",

"jslypw": "1"

}, {

"isNewRecord": true,

"comId": "1",

"socialCode": "123456",

"fmgbZs": "1",

"fmsqYx": "1",

"syxxYx": "1",

"wgsjYx": "1",

"scope": "5-10",

"totalNum": "1",

"percentageHighRights": "1",

"percentageHigh": "1",

"dspw": "1",

"jslypw": "1"

}, {

"isNewRecord": true,

"comId": "1",

"socialCode": "123456",

"fmgbZs": "1",

"fmsqYx": "2",

"syxxYx": "3",

"wgsjYx": "4",

"scope": "90-100",

"totalNum": "10"

}],

"legalList": [{

"isNewRecord": true,

"comId": "1",

"socialCode": "123456",

"underTrial": "1",

"invalid": "1",

"efficient": "1",

"totalNum": "1",

"percentage": "1"

}],

"propertyRightList": [{

"isNewRecord": true,

"comId": "1",

"socialCode": "123456",

"fmgbZs": "1",

"fmgbZsP": "1",

"fmsqYx": "1",

"fmsqYxP": "1",

"syxxYx": "1",

"syxxYxP": "1",

"wgsjYx": "1",

"wgsjYxP": "1",

"rzYx": "1",

"rzYxP": "1",

"maxPercentage": "1",

"maxPercentageRights": "1",

"dspw": "1",

"jslypw": "1"

}],

"unionPatentNum": 13,

"ipcList": [{

"isNewRecord": true,

"comId": "1",

"socialCode": "123456",

"ipcDept": "1",

"ipcClass": "1",

"ipcNum": "1",

"ipcZbClass": "1",

"ipcZb": "1",

"dspw": "1",

"jslypw": "1"

}],

"yearList": [{

"isNewRecord": true,

"comId": "1",

"fmgbZs": "1",

"fmsqYx": "1",

"syxxYx": "1",

"wgsjYx": "1",

"rzYx": "1",

"years": "2022",

"dnkjcxcgkbjshl": "1",

"socialCode": "123456"

}, {

"isNewRecord": true,

"comId": "1",

"years": "2023",

"dnkjcxcgkbjshl": "2",

"socialCode": "123456"

}, {

"isNewRecord": true,

"comId": "1",

"years": "2026",

"dnkjcxcgkbjshl": "6",

"socialCode": "123456"

}, {

"isNewRecord": true,

"comId": "1",

"years": "2027",

"dnkjcxcgkbjshl": "7",

"socialCode": "123456"

}],

"unionList": [{

"isNewRecord": true,

"comId": "1",

"socialCode": "123456",

"unionName": "Test 5",

"unionNum": "5",

"unionCompany": "1"

}, {

"isNewRecord": true,

"comId": "1",

"socialCode": "123456",

"unionName": "Test 4",

"unionNum": "4",

"unionCompany": "1"

}, {

"isNewRecord": true,

"comId": "1",

"socialCode": "123456",

"unionName": "Test 3",

"unionNum": "3",

"unionCompany": "1"

}, {

"isNewRecord": true,

"comId": "1",

"socialCode": "123456",

"unionName": "Test 1",

"unionNum": "1",

"unionCompany": "1"

}]

},

"statusCode": 200,

"header": {

"date": "Tue, 30 May 2023 07:08:28 GMT",

"powered-by": "JeeSite V5.0.1 0",

"transfer-encoding": "chunked",

"content-type": "application/json"

},

"errMsg": "request:ok"

};

let resultData = result.data;

if (resultData.yearList != undefined && resultData.yearList.length > 0) {

console.log('Execution chart 1');

// Fill chart 1 2

myIndex.fillChartOneTwo(resultData.yearList);

}

if (resultData.goldList != undefined && resultData.goldList.length > 0) {

// Fill chart 3

myIndex.fillChartThree(resultData.goldList);

}

if (resultData.propertyRightList != undefined && resultData.propertyRightList.length > 0) {

// populate chart 4

myIndex.fillChartFour(resultData.propertyRightList);

}

if (resultData.ipcList != undefined && resultData.ipcList.length > 0) {

// populate chart 5

myIndex.fillChartFive(resultData.ipcList);

}

if (resultData.patentValueList != undefined && resultData.patentValueList.length > 0) {

// Fill chart 6

myIndex.fillChartSix(resultData.patentValueList);

}

if (resultData.legalList != undefined && resultData.legalList.length > 0) {

// populate chart 7 laws

myIndex.fillChartSeven(resultData.legalList);

}

if (resultData.patentLifeList != undefined && resultData.patentLifeList.length > 0) {

// Populate chart 8 lifespan

myIndex.fillChartEight(resultData.patentLifeList);

}

if (resultData.unionList != undefined && resultData.unionList.length > 0) {

// Fill in chart 9 patent union

myIndex.fillChartNine(resultData, "GAC Group");

}

}

}

}

</script>

```

#### CSS

```CSS

<style>

.content {

display: flex;

flex-direction: column;

align-items: center;

justify-content: center;

}

.mui-content-padded {

margin: 10px 8px;

}

.mui-content-padded div {

font-size: 13px;

}

.exportPdfBtn {

width: 100px;

height: 40px;

line-height: 40px;

margin-top: 20px;

background-color: greenyellow;

text-align: center;

}

</style>

```

Guess you like

Origin blog.csdn.net/chenchuang0128/article/details/130977494
Recommended