html - 为什么在html中预览和打印时pdf不同

标签 html css google-chrome firefox jasper-reports

我目前正在尝试从 chrome 和 firefox 打印一个类型为 pdf 的 blob,它是使用 jasperreport 从我的 spring 后端服务器 api 生成的。一切都运行完美,除非 pdf 非常小,比如这张图片的宽度和高度应小于或等于 280px。它应该和收据一样宽,但长度很短 Preview

但是,在点击打印图标后,打印的内容是不同的,像这张图片那样添加了额外的空白,或者只是将这个小的 pdf 添加到整个空白 A4 页面的左上角。 enter image description here

我也在使用 Angular 和 Clarity CSS。下面是显示 blob 的代码: preview.ts

export class PdfPreviewComponent implements OnInit {

  @Input()
  title = '';

  previewUrl: string;
  open = false;
  safeUrl: SafeUrl;

  patientCode = false;

  @ViewChild('modalPreview')
  private modalPreview: ClrModal;

  constructor(private sanitizer: DomSanitizer) { }

  ngOnInit() {
    this.modalPreview._openChanged.subscribe(
      event => {
        this.open = event;
      }
    );
  }

  preview() {
     let url;
this.someService.print().subscribe(result => {
      url = URL.createObjectURL(result);
    });
    this.previewUrl = encodeURI(url);
    this.safeUrl = this.sanitizer.bypassSecurityTrustResourceUrl(this.previewUrl);
    this.open = true;
  }

preview.html

<clr-modal #modalPreview [(clrModalOpen)]="open" [clrModalSize]="'lg'" class="vertical-top">
  <h4 class="modal-title">{{title}}</h4>
  <div class="modal-body">
    <div class="bootbox-body">
      <div class="clr-row mt5">
        <iframe class="e2e-iframe-trusted-src" style="height: 85vh; width: 100%;" [src]="safeUrl"></iframe>
      </div>
    </div>
  </div>
</clr-modal>

后端 API

    MultipartFile pdf = consumerBusiness.exportCode(name, code);
    if (pdf != null) {
      ByteArrayResource resource = new ByteArrayResource(pdf.getBytes());
      HttpHeaders header = new HttpHeaders();
      header.add(HttpHeaders.CONTENT_DISPOSITION, "inline; filename=" + pdf.getOriginalFilename());
      header.add("Cache-Control", "no-cache, no-store, must-revalidate");
      header.add("Pragma", "no-cache");
      header.add("Expires", "0");
      return ResponseEntity.ok()
          .headers(header)
          .contentLength(resource.contentLength())
          .contentType(MediaType.parseMediaType("application/pdf"))
          .body(resource);
    }
    return new ResponseEntity(HttpStatus.OK);

最佳答案

除了 @media print CSS 之外,Chrome PDF 渲染可能无法识别任何其他样式。

例如你可以试试这个:

@media print {
.bootbox-body {
max-width: 300px;
margin: 0 auto;
}
}

关于html - 为什么在html中预览和打印时pdf不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59457649/

相关文章:

javascript - 谷歌地图仅在 chrome 浏览器中获取 "initMap is not a function"

css - FancyBox 关闭按钮在 chrome 中显示四个按钮

javascript - 将一个元素置于具有相同 z-index 的其他元素之前

javascript - 生成可打开链接模式窗口的可嵌入代码

javascript - 如何使用最小高度获取div高度?

javascript - 调整文本大小以完全填满容器

html - 页脚问题后的空白

jquery - Wordpress 导航中的自定义链接

html - 图像火狐浏览器不匹配

javascript - jquery .prop() 不适用于带有复选框的下拉菜单