// A re-implementation of "mutool merge" in JavaScript.
// https://mupdf.com/docs/examples/pdf-merge.js
function copyPage(dstDoc, srcDoc, pageNumber, dstFromSrc) {
  var srcPage, dstPage
  srcPage = srcDoc.findPage(pageNumber)
  dstPage = dstDoc.newDictionary()
  dstPage.Type = dstDoc.newName("Page")
  if (srcPage.MediaBox) dstPage.MediaBox = dstFromSrc.graftObject(srcPage.MediaBox)
  if (srcPage.Rotate) dstPage.Rotate = dstFromSrc.graftObject(srcPage.Rotate)
  if (srcPage.Resources) dstPage.Resources = dstFromSrc.graftObject(srcPage.Resources)
  if (srcPage.Contents) dstPage.Contents = dstFromSrc.graftObject(srcPage.Contents)
  dstDoc.insertPage(-1, dstDoc.addObject(dstPage))
}

// List of image coordinates
// [pagenum, top left, top right, bottom right, bottom left]
var images = [[0, [0, 0], [3508, 0], [3508, 5100], [0, 5100]],
[1, [814, 1004], [2670, 1004], [2670, 3558], [814, 3558]],
[2, [414, 876], [2966, 876], [2966, 1684], [414, 1684]],
[2, [444, 3530], [1504, 3530], [1504, 4284], [444, 4284]],
[2, [1828, 3528], [2944, 3528], [2944, 4219], [1828, 4219]],
[3, [2073, 168], [3158, 168], [3158, 1910], [2073, 1910]],
[3, [356, 150], [3158, 150], [3158, 944], [356, 944]],
[3, [434, 1200], [1533, 1200], [1533, 2020], [434, 2020]],
[3, [430, 2177], [1533, 2177], [1533, 2916], [430, 2916]],
[3, [2076, 2152], [3182, 2152], [3182, 3008], [2076, 3008]],
[3, [2086, 3138], [3197, 3138], [3197, 4870], [2086, 4870]],
[3, [3102, 3793], [3197, 3793], [3197, 3884], [3102, 3884]],
[3, [428, 3112], [1492, 3112], [1492, 3916], [428, 3916]],
[3, [360, 4100], [3174, 4100], [3174, 4870], [360, 4870]],
[4, [894, 1818], [1276, 1818], [1276, 2082], [894, 2082]],
[4, [902, 3442], [1286, 3442], [1286, 3694], [902, 3694]],
[4, [1244, 3538], [1286, 3538], [1286, 3586], [1244, 3586]],
[4, [902, 3694], [1056, 3694], [1056, 3724], [902, 3724]],
[4, [142, 1782], [790, 1782], [790, 5076], [142, 5076]],
[5, [1788, 636], [3304, 636], [3304, 1446], [1788, 1446]],
[6, [906, 1173], [3218, 1173], [3218, 3885], [906, 3885]],
[6, [1250, 1200], [1342, 1200], [1342, 1256], [1250, 1256]],
[6, [2672, 3802], [3218, 3802], [3218, 3884], [2672, 3884]],
[7, [177, 168], [2653, 168], [2653, 3120], [177, 3120]],
[8, [63, 204], [1324, 204], [1324, 2185], [63, 2185]],
[8, [1332, 2656], [2552, 2656], [2552, 2725], [1332, 2725]],
[8, [1332, 2654], [3146, 2654], [3146, 5061], [1332, 5061]],
[9, [175, 3194], [1419, 3194], [1419, 5006], [175, 5006]],
[9, [2168, 182], [3182, 182], [3182, 1784], [2168, 1784]],
[10, [808, 577], [2020, 577], [2020, 1636], [808, 1636]],
[10, [1990, 3630], [2836, 3630], [2836, 4214], [1990, 4214]],
[11, [820, 1447], [1548, 1447], [1548, 1544], [820, 1544]],
[11, [820, 1454], [1302, 1454], [1302, 1544], [820, 1544]],
[11, [2179, 1132], [3510, 1132], [3510, 3950], [2179, 3950]],
[11, [2538, 4422], [3510, 4422], [3510, 4734], [2538, 4734]],
[12, [0, 4416], [752, 4416], [752, 4734], [0, 4734]],
[12, [0, 1128], [1298, 1128], [1298, 3940], [0, 3940]],
[13, [298, 2262], [610, 2262], [610, 2776], [298, 2776]],
[13, [294, 2680], [2000, 2680], [2000, 3424], [294, 3424]],
[13, [282, 4633], [588, 4633], [588, 4690], [282, 4690]],
[13, [280, 4633], [1998, 4633], [1998, 4858], [280, 4858]],
[13, [280, 4719], [1998, 4719], [1998, 5078], [280, 5078]],
[13, [2086, 406], [3306, 406], [3306, 1814], [2086, 1814]],
[13, [2000, 3798], [3168, 3798], [3168, 4014], [2000, 4014]],
[13, [2762, 3828], [3168, 3828], [3168, 3937], [2762, 3937]],
[13, [2000, 3934], [3304, 3934], [3304, 5100], [2000, 5100]],
[14, [211, 190], [1471, 190], [1471, 1254], [211, 1254]],
[14, [218, 2212], [2074, 2212], [2074, 3357], [218, 3357]],
[14, [220, 3357], [2072, 3357], [2072, 3844], [220, 3844]],
[14, [2084, 3484], [2234, 3484], [2234, 3641], [2084, 3641]],
[14, [2088, 4101], [2192, 4101], [2192, 4228], [2088, 4228]],
[14, [2088, 4269], [2188, 4269], [2188, 4395], [2088, 4395]],
[14, [2088, 4497], [2382, 4497], [2382, 4576], [2088, 4576]],
[14, [2084, 3484], [3318, 3484], [3318, 5018], [2084, 5018]],
[14, [2882, 4866], [3314, 4866], [3314, 4936], [2882, 4936]],
[15, [275, 240], [1305, 240], [1305, 1444], [275, 1444]],
[15, [1994, 1422], [2620, 1422], [2620, 1426], [1994, 1426]],
[15, [1994, 1426], [2620, 1426], [2620, 1490], [1994, 1490]],
[15, [1994, 1490], [2620, 1490], [2620, 2060], [1994, 2060]],
[15, [1406, 2458], [3218, 2458], [3218, 3834], [1406, 3834]],
[16, [302, 3672], [1510, 3672], [1510, 4994], [302, 4994]],
[16, [1663, 154], [3325, 154], [3325, 1998], [1663, 1998]]];

var srcDoc = new PDFDocument(argv[1]);
var dstDoc = new PDFDocument();
var dstFromSrc = dstDoc.newGraftMap()
for (var i = 0; i < images.length; i++) {
  //clonepage
  copyPage(dstDoc, srcDoc, images[i][0], dstFromSrc);
  var page = dstDoc.findPage(dstDoc.countPages() - 1);
  var tr = images[i][2];
  var bl = images[i][4];
  var f = .24;
  print(page.MediaBox);
  var height = page.MediaBox[3] - page.MediaBox[1];
  var width = page.MediaBox[2] - page.MediaBox[0];
  print(height);
  page.MediaBox = page.CropBox = [bl[0] * f, height - bl[1] * f, tr[0] * f, height - tr[1] * f];
}
dstDoc.save(argv[2], "compress");