首页 \ 问答 \ PDFJS和PDF编码(PDFJS and PDF encoding)

PDFJS和PDF编码(PDFJS and PDF encoding)

我们正在实施PDFJS以在网站上呈现pdf文件。

当尝试将PDF文档/查看器作为arrayBuffer启动时,我们会遇到各种错误,并且不会呈现文件。 从url(DEFAULT_URL变量)打开查看器中的同一文件时,文件呈现正常。

但是有些文件会以流形式呈现。 在记事本中比较这些文件表明它们具有不同的编码/字符。

这段代码用于在查看器中打开文件:

function rawStringToBuffer( str ) {
    var idx, len = str.length, arr = new Array( len );
    for ( idx = 0 ; idx < len ; ++idx ) {
        arr[ idx ] = str.charCodeAt(idx) & 0xFF;
    }
    return new Uint8Array( arr ).buffer;
}

function readSingleFile(e) {
  var file = e.target.files[0];
  if (!file) {
    return;
  }
  var reader = new FileReader();
  reader.onload = function(e) {
    var contents = e.target.result;

    var uint8array = rawStringToBuffer(contents);

    pdfjsframe.contentWindow.PDFViewerApplication.open(uint8array,0);

    };
    reader.readAsText(file);
}

test.pdf helloworld pdf,不使用上面的代码呈现。

记事本中的文件内容

test2.pdf helloworld pdf,它使用上面的代码进行渲染。

在此处输入图像描述

该行为不依赖于浏览器。 构建是b15f335。

是否存在查看器的代码或默认配置,以便查看器无法呈现test.pdf?


We are implementing PDFJS to render pdf files on a website.

When trying to initiate a PDFdocument/Viewer as an arrayBuffer, we get al sorts of errors and the file is not rendered. When opening the same file in the viewer from url (DEFAULT_URL variable), the file renders fine.

There are however some files that do render as streams. Comparing these files in notepad shows they have different encoding/characters.

This piece of code is used to open the file in the viewer:

function rawStringToBuffer( str ) {
    var idx, len = str.length, arr = new Array( len );
    for ( idx = 0 ; idx < len ; ++idx ) {
        arr[ idx ] = str.charCodeAt(idx) & 0xFF;
    }
    return new Uint8Array( arr ).buffer;
}

function readSingleFile(e) {
  var file = e.target.files[0];
  if (!file) {
    return;
  }
  var reader = new FileReader();
  reader.onload = function(e) {
    var contents = e.target.result;

    var uint8array = rawStringToBuffer(contents);

    pdfjsframe.contentWindow.PDFViewerApplication.open(uint8array,0);

    };
    reader.readAsText(file);
}

test.pdf helloworld pdf which is not rendered with code above.

file content in notepad

test2.pdf helloworld pdf which does rendered with code above.

enter image description here

The behaviour is not browser dependent. The build is b15f335.

Is there something with the code or default configuration of the viewer so that test.pdf can not be rendered by the viewer?


原文:https://stackoverflow.com/questions/37673583
更新时间:2022-12-15 08:12

最满意答案

我不确定如何有效地使用SIMD对任意矩阵进行就地转置,但我确实知道如何在不合适的地方进行。 让我来描述如何做到这两点

到位转置

对于就地转置,您应该在C ++手册中看到Agner Fog的优化软件 。 请参见第9.10节“大型数据结构中的缓存争用”示例9.5a。 对于某些矩阵大小,由于缓存别名,您将看到性能大幅下降。 有关示例,请参见表9.1, 为什么转换512x512的矩阵要比转置513x513的矩阵慢得多? 。 Agner提供了一种方法来解决这个问题,使用示例9.5b中的循环平铺(类似于Paul R所描述的)。

不合适的转置

在这里查看我的答案(投票率最高的人) 在C ++中转换矩阵的最快方法是什么? 。 我已经很久没看过了这个,但是让我在这里重复我的代码:

inline void transpose4x4_SSE(float *A, float *B, const int lda, const int ldb) {
    __m128 row1 = _mm_load_ps(&A[0*lda]);
    __m128 row2 = _mm_load_ps(&A[1*lda]);
    __m128 row3 = _mm_load_ps(&A[2*lda]);
    __m128 row4 = _mm_load_ps(&A[3*lda]);
     _MM_TRANSPOSE4_PS(row1, row2, row3, row4);
     _mm_store_ps(&B[0*ldb], row1);
     _mm_store_ps(&B[1*ldb], row2);
     _mm_store_ps(&B[2*ldb], row3);
     _mm_store_ps(&B[3*ldb], row4);
}

inline void transpose_block_SSE4x4(float *A, float *B, const int n, const int m, const int lda, const int ldb ,const int block_size) {
    #pragma omp parallel for
    for(int i=0; i<n; i+=block_size) {
        for(int j=0; j<m; j+=block_size) {
            int max_i2 = i+block_size < n ? i + block_size : n;
            int max_j2 = j+block_size < m ? j + block_size : m;
            for(int i2=i; i2<max_i2; i2+=4) {
                for(int j2=j; j2<max_j2; j2+=4) {
                    transpose4x4_SSE(&A[i2*lda +j2], &B[j2*ldb + i2], lda, ldb);
                }
            }
        }
    }   
}

I'm not sure how to do a in-place transpose for arbitrary matrices using SIMD efficiently but I do know how to do it for out-of-place. Let me describe how to do both

In place transpose

For in-place transpose you should see Agner Fog's Optimizing software in C++ manual. See section 9.10 "Cache contentions in large data structures" example 9.5a. For certain matrix sizes you will see a large drop in performance due to cache aliasing. See table 9.1 for examples and this Why is transposing a matrix of 512x512 much slower than transposing a matrix of 513x513?. Agner gives a way to fix this using loop tiling (similar to what Paul R described) in Example 9.5b.

Out of place transpose

See my answer here (the one with the most votes) What is the fastest way to transpose a matrix in C++?. I have not looked into this in ages but let me just repeat my code here:

inline void transpose4x4_SSE(float *A, float *B, const int lda, const int ldb) {
    __m128 row1 = _mm_load_ps(&A[0*lda]);
    __m128 row2 = _mm_load_ps(&A[1*lda]);
    __m128 row3 = _mm_load_ps(&A[2*lda]);
    __m128 row4 = _mm_load_ps(&A[3*lda]);
     _MM_TRANSPOSE4_PS(row1, row2, row3, row4);
     _mm_store_ps(&B[0*ldb], row1);
     _mm_store_ps(&B[1*ldb], row2);
     _mm_store_ps(&B[2*ldb], row3);
     _mm_store_ps(&B[3*ldb], row4);
}

inline void transpose_block_SSE4x4(float *A, float *B, const int n, const int m, const int lda, const int ldb ,const int block_size) {
    #pragma omp parallel for
    for(int i=0; i<n; i+=block_size) {
        for(int j=0; j<m; j+=block_size) {
            int max_i2 = i+block_size < n ? i + block_size : n;
            int max_j2 = j+block_size < m ? j + block_size : m;
            for(int i2=i; i2<max_i2; i2+=4) {
                for(int j2=j; j2<max_j2; j2+=4) {
                    transpose4x4_SSE(&A[i2*lda +j2], &B[j2*ldb + i2], lda, ldb);
                }
            }
        }
    }   
}

相关问答

更多
  • http://gcc.gnu.org/projects/tree-ssa/vectorization.html 使用所需选项-O3 -msse2 有关更多选项,请阅读上面的文档。 http://gcc.gnu.org/projects/tree-ssa/vectorization.html Use the required options, -O3 -msse2 For more options, read the documentation above.
  • 我稍微修改了你的源代码,以确保GCC无法删除循环: #include #define n 1024 int main () { int i, a[n], b[n], c[n]; for(i=0; i
  • 看起来你正在尝试卷积。 你可以纯粹用numpy来做到这一点,就像你所展示的一样。 或者,我们可以使用内置的卷积功能: from scipy.signal import convolve2d k_y = np.array([[1, -2, 1]]).T k_x = np.array([[1, -2, 1]]) acc = np.zeros_like(grid) acc[:, :, 0] = convolve2d(grid[:, :, 0], k_y, mode='same') acc[:, :, 1] = ...
  • 方法#1 基于bsxfun的方法 - out = bsxfun(@minus,X,X').^2 + bsxfun(@minus,Y,Y').^2 < radius^2 out(1:n+1:end)= 0 方法#2 基于Distance matrix calculation using matrix-multiplication的方法Distance matrix calculation using matrix-multiplication (可能更快) - A = [X(:) Y(:)] A_t = A ...
  • 方法1:矩阵乘6D排列 % Get sizes [m1,m2,~] = size(X); [n1,n2,N,n4,n5] = size(Y); % Lose the third dim from X and Y with matrix-multiplication parte1 = reshape(permute(Y,[1,2,4,5,3]),[],N)*reshape(X,[],N).'; % Rearrange the leftover dims to bring kron format par ...
  • 从http://research.microsoft.com/en-us/um/people/simonpj/papers/ndp/haskell-beats-C.pdf中的工作中得到GHC的SIMD分支, 网址为https://ghc.haskell .org / trac / ghc / wiki / SIMD以及正在进行的工作,将这些指令作为原始版本,适用于诸如vector的库。 ( https://dorchard.wordpress.com/2013/10/14/automatic-simd-ve ...
  • 我还没有看到GCC或英特尔C ++自动向量化任何东西,但非常简单的循环,即使给定的算法代码可以(并且在我使用SSE内在函数手动重写之后)被矢量化。 其中一部分是保守的 - 特别是当遇到可能的指针别名时,C / C ++编译器很难向自己“证明”矢量化是安全的,即使你是程序员知道的那样。 大多数编译器(明智地)更喜欢不优化代码而不是冒错误编译它的风险。 这是高级语言比C更有优势的一个领域,至少在理论上是这样的(我在理论上说,因为我实际上并不知道任何自动向量化的ML或Haskell编译器)。 它的另一部分只是分析 ...
  • 如果你可以将坐标投影到局部投影(例如UTM ),这对于pyproj是非常直接的并且通常比lon / lat更有利于测量,那么使用scipy.spatial有更多更快的方法。 df['something'] = df.apply(...)和np.vectorize()都没有真正矢量化,在引擎盖下,它们使用循环。 ds1 id lon lat varA 0 1 20.11 19.88 100 1 2 20.87 18.65 90 2 3 18.99 20.7 ...
  • 你的for循环实际上非常快。 正如@EBH在评论中所提到的,您的代码应该在最新的2016版本中运行,但由于我使用的是早期的2015版本,因此不支持隐式扩展。 原始声明: rangesearch(A,A,0.01)不保证您可以为每个点获得单个邻居。 事实上,当我以N=10运行时,id总是{1 2 3 4 5 6 7 8 9 10} 固定for循环方法: tic result = zeros(N,1); for i = 1:N idx = id{i}'; v1 = bsxfun(@minus, ...
  • 我不确定如何有效地使用SIMD对任意矩阵进行就地转置,但我确实知道如何在不合适的地方进行。 让我来描述如何做到这两点 到位转置 对于就地转置,您应该在C ++手册中看到Agner Fog的优化软件 。 请参见第9.10节“大型数据结构中的缓存争用”示例9.5a。 对于某些矩阵大小,由于缓存别名,您将看到性能大幅下降。 有关示例,请参见表9.1, 为什么转换512x512的矩阵要比转置513x513的矩阵慢得多? 。 Agner提供了一种方法来解决这个问题,使用示例9.5b中的循环平铺(类似于Paul R所描 ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)