[英]Expo - reduce PDF filesize on iOS devices
我目前正在我的 Expo-App 中創建一個 A4 PDF,使用“expo-print” API ( printtofileasync )。 PDF 包含圖像(從設備拍攝的照片)和一些文本。 我已將 PDF 尺寸設置為 595 寬,842 高(A4 尺寸)。 不幸的是,PDF 的大小對於我的要求來說太大了(1.9MB,只有 1 個圖像)。
我能夠通過減小圖像大小來減小 Android 上的 PDF 大小,但這在 iOS 上不起作用。 我懷疑在 iOS Expo 上只是“制作屏幕截圖”的頁面,因此更改圖像大小沒有效果。 我已經嘗試將整個 PDF 大小減小到 A5,但這不是一個解決方案,因為 PDF 之后需要在 A4 上打印。
任何幫助,將不勝感激!
更新:目前這是我的代碼:
const { uri, base64 } = await Print.printToFileAsync({
width: 595,
height: 842,
html: 'data...',
base64: true,
});
Share.share({
url: 'data:application/pdf;base64,' + base64,
});
I have the same problem, I change my pdf creator because html creates variable size pdf and it depends on the resolution device, I use pdf-lib it works in javascript, you can create or modify pdf, I write small example in Expo, I計划創建一個庫來做附加,你可以填寫 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 文件。 我認為它應該是更小的尺寸,跟隨。
import * as Print from 'expo-print';
const pdf = Print.printToFileAsync({
width: 612, // <=== your width
height: 612, // <=== your height
base64: true
});
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 軟件都是平等的,許多創作者出生的文檔都是臃腫的,並且可以從優化中受益匪淺。
請注意,1.9MB 有點隨意,尤其是在處理數字圖像時,您可能需要根據輸入圖像的質量和數量與利益相關者重新考慮該要求。
我公司提供了一個名為 PDF Optimizer的工具,它可以提供幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.