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

我目前正在使用“ 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,});
QQ516061641 回答:博览会-减少iOS设备上的PDF文件大小

我有同样的问题,我更改了pdf创建器,因为html创建了可变大小的pdf,并且它取决于分辨率设备,我使用pdf-lib可以在javascript中使用,可以创建或修改pdf,我写的很小以Expo为例,我打算创建一个图书馆 另外,您可以fill 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"
  }
});
,

您可以尝试使用base64字符串并将其解码回pdf文件。我认为它的尺寸应该较小,请继续。

  1. 使用Expo Print API获取有问题的pdf文件,请记住将base64设置为true。
import * as Print from 'expo-print';

const pdf = Print.printToFileAsync({
    width: 612,// <=== your width
    height: 612,// <=== your height
    base64: true
});
  1. 您可以使用thisthis库来解码回base64文件。
import * as Print from 'expo-print';
import base64 from 'base64'; // or import base64 from 'react-native-base64'

const pdf = Print.printToFileAsync({
     width: 612,// <=== your width
     height: 612,// <=== your height
     base64: true
});

const pdfDecoded = base64(pdf);

pdfDecoded将保存您的新pdf文件,并且文件应较小,您可以将其发送到服务器或显示它。

我还没有测试这2美分。

希望这会有所帮助!

,

如果您需要拍摄数码照片并将其添加到PDF文档中,并且对PDF文件的大小有限制,则在开始尝试缩小页面大小以缩小尺寸之前,该文档...。我强烈建议您考虑优化创建的PDF。

并非所有PDF软件都是平等创建的,许多创建者出生的文档that肿且可以从优化中受益匪浅。

请注意,1.9MB有点随意,尤其是在处理数字图像时,您可能还需要根据输入图像的质量和数量与利益相关者重新考虑这一要求。

我的公司提供了一个名为PDF Optimizer的工具,可以提供帮助。

本文链接:https://www.f2er.com/3151716.html

大家都在问