61 lines
2.2 KiB
JavaScript
61 lines
2.2 KiB
JavaScript
const fs = require('fs');
|
||
const path = require('path');
|
||
const fetch = require('node-fetch'); // fetchを使用するためにnode-fetchをインポート
|
||
|
||
// 送信先サーバーのURLとAPIキー
|
||
const SERVER_URL = 'https://neo999.next-hd.net:30309/pdfMerge'; // サーバーのURLに合わせて変更
|
||
const API_KEY = 'pgqhLFWbDFu4Byz#4afNYX2F6Fa1&$KPjved$8%sUdTQV52caip#EpIKxYUkdd4S'; // サーバーと同じAPIキー
|
||
|
||
|
||
// ローカルPDFファイルのパスを指定
|
||
const pdfFile1 = path.join(__dirname, 'file1.pdf');
|
||
const pdfFile2 = path.join(__dirname, 'file2.pdf');
|
||
|
||
// PDFファイルをサーバーに送信してマージする関数
|
||
async function sendPDFsForMerge() {
|
||
try {
|
||
// PDFファイルを読み込む
|
||
const pdfData1 = fs.readFileSync(pdfFile1);
|
||
const pdfData2 = fs.readFileSync(pdfFile2);
|
||
const fileName = path.basename(pdfFile1); // ファイル名を取得(file1.pdfの名前を使用)
|
||
|
||
// Base64エンコード
|
||
const base64Pdf1 = pdfData1.toString('base64');
|
||
const base64Pdf2 = pdfData2.toString('base64');
|
||
|
||
// リクエストボディを作成
|
||
const pdfDataJson = {
|
||
base64_1: base64Pdf1,
|
||
base64_2: base64Pdf2,
|
||
fileName: fileName // マージ後のファイル名を指定
|
||
};
|
||
|
||
// fetchを使用してサーバーにPOSTリクエストを送信
|
||
const response = await fetch(SERVER_URL, {
|
||
method: 'POST',
|
||
headers: {
|
||
'Content-Type': 'application/json',
|
||
'x-api-key': API_KEY
|
||
},
|
||
body: JSON.stringify(pdfDataJson)
|
||
});
|
||
|
||
if (!response.ok) {
|
||
throw new Error(`HTTPエラー: ${response.status}`);
|
||
}
|
||
|
||
// レスポンスからPDFデータをバイナリ形式で取得
|
||
const pdfBuffer = await response.buffer();
|
||
|
||
// マージされたPDFをファイルに保存
|
||
const mergedPdfPath = path.join(__dirname, 'merged.pdf');
|
||
fs.writeFileSync(mergedPdfPath, pdfBuffer);
|
||
|
||
console.log(`マージされたPDFを保存しました: ${mergedPdfPath}`);
|
||
} catch (error) {
|
||
console.error('エラーが発生しました:', error.message);
|
||
}
|
||
}
|
||
|
||
// 関数を実行
|
||
sendPDFsForMerge(); |