博览会-在iOS设备上减少PDF文件大小

ben*_*ikt 5 pdf ios react-native expo

我目前正在使用“ expo-print” API(printtofileasync)在Expo-App中创建A4 PDF 。PDF包含图像(从设备拍摄的照片)和一些文本。我将PDF大小设置为595宽度,842高度(A4尺寸)。不幸的是,PDF的大小对于我的要求来说太大(只有1张图像,只有1,9MB)。

我能够通过减小图像大小来减小Android上的PDF大小,但这在iOS上不起作用。我怀疑在iOS Expo上只是“制作屏幕截图”,因此更改图像大小没有任何效果。我已经尝试将整个PDF尺寸减小到A5,但这不是解决方案,因为之后需要在A4上打印PDF。

任何帮助,将不胜感激!

更新:目前这是我的代码:

const { uri, base64 } = await Print.printToFileAsync({
    width: 595,
    height: 842,
    html: 'data...',
    base64: true,
});

Share.share({
    url: 'data:application/pdf;base64,' + base64,
});
Run Code Online (Sandbox Code Playgroud)

Mik*_*gas 2

我有同样的问题,我更改了我的pdf创建器,因为html创建了可变大小的pdf并且它取决于分辨率设备,我使用pdf-lib它在javascript中工作,你可以创建或修改pdf,我在Expo中写了一个小例子,我计划创建一个库来做额外的事情,你可以填写PDF 注意:它类似于react-native-pdf-lib,但仅在环境javascript中工作

我的 App.tsx 示例:

import React from "react";
import { StyleSheet, View, Text, Button } from "react-native";
import { Asset } from "expo-asset";
import * as Print from "expo-print";
import { degrees, PDFDocument, rgb, StandardFonts } from "pdf-lib";

export default function App({ context }: any) {
  return (
    <View style={styles.container}>
      <Button
        title="Generate PDF"
        onPress={async () => {
          const req = new XMLHttpRequest();
          /* const templateUri = Asset.fromModule(
            require("./assets/template.pdf")
          );
          console.log(templateUri); */
          const url =  'https://pdf-lib.js.org/assets/with_update_sections.pdf' // templateUri.uri
          req.open("GET", url, true);
          req.responseType = "blob";
          /*   req.onprogress = e => (t.progress = e.loaded); */
          req.onload = () => {
            const blob = req.response;
            var reader = new FileReader();
            reader.readAsDataURL(blob);
            reader.onloadend = async function() {
              const base64data = reader.result as string; // template pdf in base64
              const pdfDoc = await PDFDocument.load(base64data);
              const helveticaFont = await pdfDoc.embedFont(
                StandardFonts.Helvetica
              );

              const pages = pdfDoc.getPages();
              const firstPage = pages[0];
              const { width, height } = firstPage.getSize();
              console.log(width, height);
              firstPage.drawText('This text was added with JavaScript!', {
                x: 5,
                y: height / 2 + 300,
                size: 50,
                font: helveticaFont,
                color: rgb(0.95, 0.1, 0.1),
                rotate: degrees(-45),
              });

              const pdfDataUri = await pdfDoc.saveAsBase64({ dataUri: true });
              Print.printAsync({ uri: pdfDataUri });
            };
          };
          req.onerror = console.error;
          req.send();
        }}
      />
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: "#fff",
    alignItems: "center",
    justifyContent: "center"
  }
});
Run Code Online (Sandbox Code Playgroud)