如何使用 zlib 在 React-Native 中解码 FlateDecoded PDF 流?

问题描述 投票:0回答:0

亲爱的 StackOverflow 社区,

我在尝试重写 PDF 文件时遇到 zlib 问题。 具体来说,我试图将 FlateDecoded 数据转换回正常数据,但在使用 zlib.inflate 函数时,我不断遇到 “错误的标头检查” 错误。

我的最终目标是解密FlateDecoded的数据流。 我尝试了多种方法,包括使用

  • {deflate, inflate} 来自 'react-native-gzip',
  • pako图书馆,
  • zlib 来自“react-zlib-js”, 但问题仍然存在。

我尝试了不同的策略,例如将数据转换为 UInt8Array 并使用 zlib 对其进行膨胀,以及将其转换为十六进制然后尝试对其进行膨胀。 不幸的是,这些尝试都没有解决问题,我仍然收到相同的错误。 我也尝试在 Swift 中执行此操作,但得到相同的输出和相同的错误。

这是我当前代码的片段:

import RNFS from 'react-native-fs';
import RNFetchBlob from 'rn-fetch-blob';
import { PDFDocument, } from 'pdf-lib'; 
import base64 from 'react-native-base64'; 
var Buffer = require('buffer').Buffer; 
import zlib from 'react-zlib-js'; 
var textEncoding = require('text-encoding'); 


export function rewritePDF(path) {
  return new Promise((resolve, reject) => {
    RNFS.readFile(path.replace('%20', ' '), 'base64').then(data => {
      const myNewPDFData = base64.decode(data);
      console.log(data);
      console.log(myNewPDFData);
      const pdfObjects = myNewPDFData.split(/(?<=\bobj\b)|(?=\bendobj\b)/g);
      console.log(pdfObjects);
      const flateDecodedData = pdfObjects[21];
      console.log(flateDecodedData);

      const pureStream = flateDecodedData.split(
        /(?<=\bstream\b)|(?=\bendstream\b)/g,
      )[1];
      let input = new Buffer(pureStream);
      console.log(input);
      console.log(pureStream);

      // zlib.inflate(pureStream, function (r, e) {
      //   if (e) {
      //     console.log(e);
      //     return;
      //   }
      //   console.log(r);
      // });

      // const hexPureStream = Buffer.from(pureStream, 'utf8').toString('hex');
      // console.log(hexPureStream.slice(2, hexPureStream.length - 2));

      // zlib.inflate(
      //   hexPureStream.slice(2, hexPureStream.length - 2),
      //   function (r, e) {
      //     if (e) {
      //       console.log(e);
      //       return;
      //     }
      //     console.log(r);
      //   },
      // ),
      //   zlib.deflate('test', function (e, r) {
      //     if (e) {
      //       console.log(e);
      //       return;
      //     }
      //     console.log(new TextDecoder().decode(r));

      //     zlib.inflate(r, function (err, res) {
      //       if (err) {
      //         console.log(e);
      //         return;
      //       }
      //       console.log(res);
      //       console.log(new TextDecoder().decode(res));
      //     });
      //   });

      let newData = base64.encode(myNewPDFData);
      PDFDocument.load(newData).then(async pdfDoc => {
        const pdfBytes = await pdfDoc.saveAsBase64();
        RNFetchBlob.fs
          .writeFile(
            path.replace('file://', '').replace('%20', ' '),
            pdfBytes,
            'base64',
          )
          .then(() => resolve(path))
          .catch(error => {
            console.log(error);
          });
      });
    });
  });
}

The 'data' from RNFS.readFile looks like this

The 'myNewPDFData' or decoded data looks like this

This is some of pdfObjects

This is one of FlatDecoded data

And this is pureStream

我也检查过

zlib 标头是什么样的?

zlib解压失败

还有一些关于“如何解码 FlateDecoded 数据”主题的链接。

我将非常感谢任何指导、见解或替代方法来成功解密我的案件。 React-native pdf zlib 解码

react-native pdf zlib decoding
© www.soinside.com 2019 - 2024. All rights reserved.