jinxia.mo преди 2 години
родител
ревизия
a35df7d763

+ 1 - 0
public/collectImgNew/generate_questionImg.sh

@@ -0,0 +1 @@
+node index.js --store=true --maxFile=100 --headless=true

+ 1 - 0
public/collectImgNew/generate_report.sh

@@ -0,0 +1 @@
+node index.js --url=https://front.mingxuejinbang.com/elective/report/index?token=Bearer%20eyJhbGciOiJIUzUxMiJ9.eyJsb2dpbl91c2VyX2tleSI6ImE3M2Q3ZmY1LTI2YjMtNGJkMi1hNWIxLTBhMzM2NzhiM2M1YyJ9.uqNLm6XFhUuA1GEp4FU9I7Z_tB4dR2jEBh6pzFvwgcibdcCZ1YJcP4MX_aUyfwBD5FelYJWZUHe1v5B4VipHjg --path=2022选科报告.pdf

+ 51 - 0
public/collectImgNew/html2pdf.js

@@ -0,0 +1,51 @@
+const fs = require('fs');
+const path = require('path');
+
+const args = require('minimist')(process.argv.slice(2));
+
+const headless = args.headless || 'true';
+const url = args.url || 'true';
+const token = args.token || '';
+const filePath = args.path || 'index.pdf';
+
+const puppeteer = require('puppeteer');
+
+const saveScreenshot = async (url) => {
+  if (!url) {
+    browser.close();
+    return;
+  }
+  // 启动浏览器
+  const browser = await puppeteer.launch({
+    headless: headless === 'true',
+    devtools: false
+  });
+  // 打开页面
+  const page = await browser.newPage();
+  // 设置浏览器视窗
+  page.setViewport({
+    width: 1123,
+    height: 1080,
+  })
+  // 地址栏输入网页地址
+  await page.goto(url, {
+    // 等界面加载完
+    waitUntil: 'networkidle0'
+  });
+  await page.pdf({
+    path: filePath,
+    printBackground: true,
+    displayHeaderFooter: false,
+    margin: {
+      top: '2.54cm',
+      bottom: '2.54cm',
+      left: '1.27cm',
+      right: '1.27cm'
+    },
+    format: 'A4'
+  });
+  // 关闭浏览器
+  await browser.close();
+};
+
+saveScreenshot(`${url}?token=${token}`);

+ 32 - 65
public/collectImgNew/index.js

@@ -1,85 +1,52 @@
-/**
- * 在无头浏览器中将一个网页截图保存为图片
- */
-const fs = require('fs')
-const path = require('path')
-const axios = require('axios')
-const FormData = require('form-data')
+const fs = require('fs');
+const path = require('path');
+
+const args = require('minimist')(process.argv.slice(2));
+
+const headless = args.headless || 'true';
+const url = args.url || 'true';
+const token = args.token || '';
+const filePath = args.path || 'index.pdf';
 
 const puppeteer = require('puppeteer');
 
 const saveScreenshot = async (url) => {
+  if (!url) {
+    return;
+  }
   // 启动浏览器
   const browser = await puppeteer.launch({
-    headless: true
+    args: ['--no-sandbox'],
+    headless: headless === 'true',
+    devtools: false
   });
   // 打开页面
   const page = await browser.newPage();
   // 设置浏览器视窗
   page.setViewport({
-    width: 1920,
+    width: 1123,
     height: 1080,
-  })
+  });
   // 地址栏输入网页地址
   await page.goto(url, {
     // 等界面加载完
     waitUntil: 'networkidle0'
   });
-  capchaAndUpload(page);
+  await page.addStyleTag({ path: './style.css' });
+  await page.pdf({
+    path: filePath,
+    printBackground: true,
+    displayHeaderFooter: false,
+    margin: {
+      top: '1.27cm',
+      bottom: '1.27cm',
+      left: '1.27cm',
+      right: '1.27cm'
+    },
+    format: 'A4'
+  });
   // 关闭浏览器
-  // await browser.close();
+  await browser.close();
 };
 
-saveScreenshot('file:////Users/admin/Documents/D Disk/git/mingxue/testevaluation/code/test/test-ui/public/collectImgNew/index.html');
-
-async function capchaAndUpload(page) {
-  await page.waitForSelector('#title');
-  let store = await page.evaluate(() => JSON.stringify(localStorage));
-  store = JSON.parse(store)
-  console.log('------------------' + store.questionId + '请求成功!-------------------')
-  let clip = await page.evaluate((scope) => {
-    let {
-      x,
-      y,
-      width,
-      height
-    } = document.getElementById('title').getBoundingClientRect();
-    window.scope = scope;
-    return {
-      x,
-      y,
-      width,
-      height
-    };
-  });
-  const filename = `./preview/${store.questionId}.png`;
-  const options = {
-    path: filename,
-    fullPage: false,
-    clip
-  }
-  await page.screenshot(options).then(res => {
-    let base64 = Buffer.from(res).toString('base64');//转为base64编码字符串
-    const formData = new FormData()
-    formData.append('questionId', store.questionId)
-    formData.append('imageBase64', base64)
-    upload(page, formData, store.questionId);
-  });
-}
-
-function upload(page, formData, questionId) {
-  const reqUrl = 'https://front.mingxuejinbang.com/prod-api/front/questionCollection/uploadQuestionImage';
-  axios.post(reqUrl, formData)
-    .then((res) => {
-      if (res.data.code === 200) {
-        console.log(questionId + '上传成功!')
-        // 获取下一个问题
-        page.click('#next');
-        setTimeout(() => {
-          capchaAndUpload(page);
-        }, 100)
-      } else {
-        console.log('出错了')
-      }
-    });
-}
+saveScreenshot(`${url}`);

+ 85 - 0
public/collectImgNew/index_question.js

@@ -0,0 +1,85 @@
+/**
+ * 在无头浏览器中将一个网页截图保存为图片
+ */
+const fs = require('fs')
+const path = require('path')
+const axios = require('axios')
+const FormData = require('form-data')
+
+const puppeteer = require('puppeteer');
+
+const saveScreenshot = async (url) => {
+  // 启动浏览器
+  const browser = await puppeteer.launch({
+    headless: true
+  });
+  // 打开页面
+  const page = await browser.newPage();
+  // 设置浏览器视窗
+  page.setViewport({
+    width: 1920,
+    height: 1080,
+  })
+  // 地址栏输入网页地址
+  await page.goto(url, {
+    // 等界面加载完
+    waitUntil: 'networkidle0'
+  });
+  capchaAndUpload(page);
+  // 关闭浏览器
+  // await browser.close();
+};
+
+saveScreenshot('file:////Users/admin/Documents/D Disk/git/mingxue/testevaluation/code/test/test-ui/public/collectImgNew/index.html');
+
+async function capchaAndUpload(page) {
+  await page.waitForSelector('#title');
+  let store = await page.evaluate(() => JSON.stringify(localStorage));
+  store = JSON.parse(store)
+  console.log('------------------' + store.questionId + '请求成功!-------------------')
+  let clip = await page.evaluate((scope) => {
+    let {
+      x,
+      y,
+      width,
+      height
+    } = document.getElementById('title').getBoundingClientRect();
+    window.scope = scope;
+    return {
+      x,
+      y,
+      width,
+      height
+    };
+  });
+  const filename = `./preview/${store.questionId}.png`;
+  const options = {
+    path: filename,
+    fullPage: false,
+    clip
+  }
+  await page.screenshot(options).then(res => {
+    let base64 = Buffer.from(res).toString('base64');//转为base64编码字符串
+    const formData = new FormData()
+    formData.append('questionId', store.questionId)
+    formData.append('imageBase64', base64)
+    upload(page, formData, store.questionId);
+  });
+}
+
+function upload(page, formData, questionId) {
+  const reqUrl = 'https://front.mingxuejinbang.com/prod-api/front/questionCollection/uploadQuestionImage';
+  axios.post(reqUrl, formData)
+    .then((res) => {
+      if (res.data.code === 200) {
+        console.log(questionId + '上传成功!')
+        // 获取下一个问题
+        page.click('#next');
+        setTimeout(() => {
+          capchaAndUpload(page);
+        }, 100)
+      } else {
+        console.log('出错了')
+      }
+    });
+}

+ 0 - 0
public/collectImgNew/index0.js → public/collectImgNew/re.js


+ 18 - 0
public/collectImgNew/style.css

@@ -0,0 +1,18 @@
+.fx-row.fx-bet-cen.pl12.pr12.relative,
+.navbar {
+  display: none !important;
+}
+
+body {
+  width: 1050px !important;
+  height: fit-content !important;
+  margin: auto;
+}
+
+.main-container.hasTagsView {
+  padding-top: 0 !important;
+}
+
+.elective-report-container {
+  width: 100% !important;
+}